转换的警告.类错误
Conversion warning. and Class error
我正在学习c++,在我现在正在做的作业中,我得到了一堆警告,我怀疑它们也导致了我得到的两个错误。问题是,显示警告的行是他给我们的行(其中之一),所以我认为代码一定是正确的。这使我相信在我的类声明或构造函数中存在问题。有人能发现什么问题吗?
警告(对于fillSystemCommandList函数的每一行)是不支持从字符串文字到'char *'的转换
,错误为架构x86_64的未定义符号:"COMMAND::COMMAND(char*, int)",引用自:在system_utilities.o中填充systemcommandlist ()没有找到架构x86_64的Ld:符号clang: error: link command failed with exit code 1(使用-v查看调用)
你可以跳过ParseCommandLine函数,我认为它很好。我只是包含它,以便我可以拥有整个文件(这不是我的主要btw)。
其他笔记:- systemCommands数组应该是长度为NUMBER_OF_COMMANDS的命令指针(我想我做对了)
- fillSystemCommandList函数应该用结构体填充systemCommands数组,这些结构体包含指向命令字符串的指针和相应的定义常量。
-I知道这段代码几乎全是C语言,这也是因为我选的是c++入门类。
#include "system_utilities.h"
#include "definitions.h"
using namespace std;
int getCommandNumber(char *s);
class COMMAND {
char* pointertochar;
int* pointertoint;
public:
COMMAND(char*, int);
int amIThisCommand(char*);
};
int COMMAND::amIThisCommand(char* command){
return 0;
}
COMMAND* systemCommands[NUMBER_OF_COMMANDS];
int parseCommandLine(char cline[], char *tklist[]){
int i;
int length; //length of line
int count = 0; //counts number of tokens
int toklength = 0; //counts the length of each token
length = strlen(cline);
for (i=0; i < length; i++) { //go to first character of each token
if (((cline[i] != ' ' && cline[i-1]==' ') || i == 0)&& cline[i]!= '"') {
while ((cline[i]!=' ')&& (cline[i] != ' ') && (cline[i] != 'r')){
toklength++;
i++;
}
//---------------
tklist[count] = (char *) malloc( toklength +1);
memcpy(tklist[count], &cline[i-toklength], toklength);
tklist[count][toklength]=' ';
//cout << "n" << tklist[count] << "n";
//cout << "n" << i << "n";
//cout << "n" << toklength << "n";
//--------------
count ++;
toklength = 0;
}
if (cline[i] == '"') {
do {
toklength++;
i++;
/*if (cline[i] == ' ') {
toklength--;
}*/
} while (cline[i]!='"');
//--------------
tklist[count] = (char *) malloc( toklength +1);
memcpy(tklist[count], &cline[i-toklength+1], toklength-1);
tklist[count][toklength]=' ';
//cout << "n" << tklist[count] << "n";
//cout << "n" << i << "n";
//cout << "n" << toklength << "n";
//--------------
count ++;
toklength = 0;
}
}
return count;
}
int getCommandNumber(char *s) {
/*switch (*s) {
// case "halt":
return HALT;
break;
default:
break;
}*/
return 0;
}
void fillSystemCommandList() {
systemCommands[0] = new COMMAND("halt", HALT);
systemCommands[1] = new COMMAND("status", STATUS);
systemCommands[2] = new COMMAND("time_click", TIME_CLICK);
systemCommands[3] = new COMMAND("new_sensor", NEW_SENSOR);
systemCommands[4] = new COMMAND("new_sensor_node", NEW_SENSOR_NODE);
systemCommands[5] = new COMMAND("new_network", NEW_NETWORK);
systemCommands[6] = new COMMAND("add_sensor_to_node", ADD_SENSOR_TO_NODE);
systemCommands[7] = new COMMAND("add_node_to_network", ADD_NODE_TO_NETWORK);
systemCommands[8] = new COMMAND("sensor_command", SENSOR_COMMAND);
}
再次感谢你能给予的任何帮助!
systemCommands
是COMMAND
对象的一个类。COMMAND
类中有char*
类型的成员,
"halt"
systemCommands[0] = new COMMAND("halt", HALT);
是const char*
的,因此您得到了警告消息。您没有定义COMMAND
类的构造函数。
COMMAND(const char*, int); //needs to be defined. note ptr is const
因此,当您执行
时,您会得到错误: systemCommands[0] = new COMMAND("halt", HALT);
尝试用prototype: COMMAND(char*, int);
调用构造函数
- 我在 C++ 代码中遇到错误警告:控制到达非空函数 [-Wreturn 类型] 的末尾
- G++ 编译器未为未定义的方法生成错误/警告
- 一个语句中没有多个 cout 的错误/警告
- Clang:覆盖之前在命令行上指定的所有警告和错误警告标志
- 放置新位置的错误警告
- Xcode 8.3.2 中的错误警告:文件的非可移植路径 - 指定的路径与磁盘上的文件名不同
- 编译错误:(警告:控制到达非空函数 [-wreturn 类型] 的末尾)
- snprintf:相同的代码-在不同的g++编译器上有不同的错误/警告
- 有没有办法自定义编译错误/警告消息
- 获取错误:警告C4715::并非所有控制路径都返回值,但不确定原因
- 导致.exe错误警告
- 如何在 SysntasticCheck for vim 中显示所有" g++ -Wall "错误警告?
- 开发基本的C++代码+检查错误/警告
- 如何使g++为int i=i生成错误/警告
- 我得到以下错误:[警告]多字符字符常量
- C++错误:警告:多字符字符常量/a用于用法
- C++ 在开关中隐式跌倒时强制编译时错误/警告
- 打印矩阵时,C ++程序停止,没有错误警告
- Xcode typedef 的 struct 创建错误/警告
- 主函数给出错误警告