gpt4 book ai didi

c++ - 计算器.cpp :48:24: error: invalid conversion from 'char' to 'const char*' [-fpermissive]

转载 作者:行者123 更新时间:2023-11-28 07:00:07 25 4
gpt4 key购买 nike

这是我的披露:我正在做一个学校项目,在该项目中我使用链接堆栈来实现后缀计算器。

我今天的问题是我的代码中出现了一个奇怪的(对我来说)错误,我似乎无法解决。随着时间的推移,我可以弄清楚的逻辑是我需要处理的这些句法问题(而且我认为通过练习会自然而然地出现)。

无论如何,我想知道导致此错误的原因以及原因。根据我的研究,它与字符串和字符比较有关,但这是我知识的限制。

更新:我在代码中出现的错误旁边添加了一些星号

我在学校的编译器上得到的错误 block 如下:

In file included from Calculator.h:35:0,
from CalculatorMain.cpp:6:
Calculator.cpp: In member function 'void Calculator::performOp(const string&)':
Calculator.cpp:48:24: error: invalid conversion from 'char' to 'const char*' [-fpermissive]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/bits/basic_string.tcc:214:5: error: initializing argument 1 of 'std::basic_string<_CharT, aits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' [-fpermissive]
Calculator.cpp:52:30: error: invalid conversion from 'char' to 'const char*' [-fpermissive]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/bits/basic_string.tcc:214:5: error: initializing argument 1 of 'std::basic_string<_CharT, aits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' [-fpermissive]
Calculator.cpp:53:101: error: invalid conversion from 'char' to 'const char*' [-fpermissive]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/bits/basic_string.tcc:214:5: error: initializing argument 1 of 'std::basic_string<_CharT, aits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' [-fpermissive]
Calculator.cpp:57:27: error: invalid conversion from 'char' to 'const char*' [-fpermissive]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/bits/basic_string.tcc:214:5: error: initializing argument 1 of 'std::basic_string<_CharT, aits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' [-fpermissive]
Calculator.cpp:60:23: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
Calculator.cpp:61:27: error: invalid conversion from 'char' to 'const char*' [-fpermissive]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/bits/basic_string.tcc:214:5: error: initializing argument 1 of 'std::basic_string<_CharT, aits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' [-fpermissive]
Calculator.cpp:64:23: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]

我的计算器类中的代码(对于 calculator.cpp。头文件是单独的):

** @file Calculator.cpp */

#include <string>
#include <cassert>
#include "Calculator.h"

Calculator::Calculator(){
} // end default constructor

bool Calculator::isOperator(const string& newEntry) {
string ops[] = {"-", "+", "/", "*"};

for (int i = 0; i < 4; i++) {
if (newEntry == ops[i]) {
return true;
}
}
return false;
}

bool Calculator::isOperand(const string& newEntry) {
if (!isOperator(newEntry) && newEntry != "(" && newEntry != ")") {
return true;
}
return false;
}

int Calculator::compareOps(const string& op1,const string& op2) {
if ((op1 == "*" || op1 == "/") && (op2 == "+" || op2 == "-")) {
return -1;
}
else if ((op1 == "+" || op1 == "-") && (op2 == "*" || op2 == "/")) {
return 1;
}
return 0;
}

void Calculator::performOp(const string& userExpression) {
string temp = userExpression;

int i = 0;
cout << "Error log: User expression is now " << temp << endl;
while(temp[i] > temp.length()) {
cout << "Error log: begin while loop" << endl;
**if (isOperand(temp[i])) {**
postfixString += temp[i];
}

else if **(isOperator(temp[i]))** {
**while (!infixStack.isEmpty() && infixStack.peek() != "(" && compareOps(infixStack.peek(), temp[i]) <= 0) {**
postfixString += infixStack.peek();
infixStack.pop();
}// end while
infixStack.push(temp[i]);
}

**else if (temp[i] == "(") {**
infixStack.push(temp[i]);
}

**else if (temp[i] == ")") {**
while (!infixStack.isEmpty()) {

if (infixStack.peek() == "(") {
infixStack.pop();
break;
}

postfixString += infixStack.peek();
infixStack.pop();
}//end while
}//end last else if
i++;
}//end while

while(!infixStack.isEmpty()) {
postfixString += infixStack.peek();
infixStack.pop();
}

cout << "The Postfix form of your expression is: " << postfixString << endl;
}

这是我测试这个类的文件:

#include <iostream>
#include <string>
#include "Calculator.h"

using namespace std;

int main()
{
Calculator aCalculator;
string userString = "1+1";
cout << "Testing the Link-Based Stack:" << endl;
aCalculator.performOp(userString);

return 0;
} // end main

最佳答案

您的某些函数需要字符串,但您传递的是单个字符。例如,isOperator 应该是:

bool Calculator::isOperator(char newEntry) {
char ops[] = {'-', '+', '/', '*'};

for (int i = 0; i < 4; i++) {
if (newEntry == ops[i]) {
return true;
}
}
return false;
}

因为您这样调用它:isOperator(temp[i]) 并且 temp[i] 返回一个字符,而不是一个字符串。

还有许多其他地方需要混合使用字符和字符串。

关于c++ - 计算器.cpp :48:24: error: invalid conversion from 'char' to 'const char*' [-fpermissive],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22600541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com