【问题标题】:Conversion warning. and Class error转换警告。和类错误
【发布时间】:2013-05-18 21:06:22
【问题描述】:

我正在学习 C++,在我现在正在做的一项作业中,我收到了一堆警告,我怀疑这些警告也导致了我遇到的两个错误。问题是出现警告的行是他给我们的行(其中之一),所以我认为代码必须是正确的。这让我相信我的类声明或构造函数中存在问题。有人能看出哪里不对吗?

警告(对于 fillSystemCommandList 函数的每一行)是 从字符串文字转换为 'char *' 已弃用

错误是 架构 x86_64 的未定义符号: “COMMAND::COMMAND(char*, int)”,引用自: system_utilities.o 中的 fillSystemCommandList() ld:未找到架构 x86_64 的符号 clang: error: 链接器命令失败,退出代码为 1(使用 -v 查看调用)

您可能可以跳过 ParseCommandLine 函数,我认为这一切都很好。我只是将它包含在内,以便我可以拥有整个文件(这不是我的主要内容。)

其他说明: -systemCommands 数组应该是长度为 NUMBER_OF_COMMANDS 的 COMMAND 指针(我认为我做得对)

-fillSystemCommandList 函数应该使用包含指向命令字符串的指针和相应定义的常量的结构填充 systemCommands 数组。

-我知道这段代码几乎都是 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] != '\0') && (cline[i] != '\r')){
                toklength++;
                i++;
            }
          //---------------
        tklist[count] = (char *) malloc( toklength +1);
        memcpy(tklist[count], &cline[i-toklength], toklength);
        tklist[count][toklength]='\0';
            //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]='\0';
            //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);

}

再次感谢您提供的任何帮助!

【问题讨论】:

    标签: c++ arrays class object pointers


    【解决方案1】:

    systemCommandsCOMMAND 对象的一个​​类。 COMMAND 类有 char* 类型的成员,但是:"halt" in

       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);
    

    尝试使用原型调用构造函数:COMMAND(char*, int);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-24
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 2020-07-22
      相关资源
      最近更新 更多