gpt4 book ai didi

c++ - "undefined reference to PrintArgv": why?

转载 作者:太空宇宙 更新时间:2023-11-04 15:50:04 24 4
gpt4 key购买 nike

当我编译我的程序时,我得到了三个“未定义对‘PrintArgv(...)’的引用”错误。我进行了搜索,但找不到出现这些错误的原因。这是我的代码:

    #include "tools.hpp"

enum ARG_T {COMMAND, SWITCH, ARGUMENT};
void ReadArgs(int, char**, ofstream&);
void PrintArgv(char*, ARG_T, ofstream&);


int main(int argc, char* argv[])
{
ofstream fout;
try{
fout.open("P1Ford.txt", ios::out | ios::app);
}
catch(...)
{
//fatal("Could not open file P1Ford.txt");
cout << "Could not open file P1Ford.txt" << "\n";
return -1;
}

ReadArgs(argc, argv, fout);

fout.close();
return 0;
}

/*-----------------------------------------------------------------------------
This function takes the values passed in through the command line
then examines each one to determine if it is the command, a switch or
an argument. Then passes it to a function to print it to the file.
-----------------------------------------------------------------------------*/
void ReadArgs(int argc, char* argv[], ofstream& fout)
{
for(int c=0; c<argc; ++c)
{
if(c == 0) PrintArgv(argv[c], COMMAND, fout);
else if(strncmp(argv[c], "-", 1) == 0) PrintArgv(argv[c], SWITCH, fout);
else if(strncmp(argv[c], ">", 1) == 0) ;
else PrintArgv(argv[c], ARGUMENT, fout);
}
}

/*-----------------------------------------------------------------------------
This function prints the argument in the correct format
Command <command>
Switch <switch>
Argument <agrument>
Argument <argument>
-----------------------------------------------------------------------------*/
void PrintArgv(char* val, ARG_T type, ostream& fout)
{
if(type == COMMAND) fout << "Command " << val << "\n";
if(type == SWITCH) fout << "\t" << "Switch " << val << "\n";
if(type == ARGUMENT) fout << "\t" << "Argument " << val << "\n";
}

函数 ReadArgs 是我遇到错误的地方。每次调用 PrintArgv 都会给我错误。我正在使用 G++ 4.6.1。

最佳答案

您的原型(prototype)有一个类型为 ofstream & 的参数,您的 header 有一个类型为 ostream & 的参数。这意味着 PrintArgv 的实现实际上是在声明一个新函数。但是,您将指定的函数称为原型(prototype),但它没有函数体,因为您无意中创建了一个新函数,而不是定义原始函数的函数体。

关于c++ - "undefined reference to PrintArgv": why?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10051004/

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