C++错误:未定义的符号
C++ Error: Undefined Symbol
我的代码的目的是能够在文本中的某个地方输入一个带有空白-n的文件。假设将 blank-N 更改为用户提示的代码,例如 (blank-N) =noun 并要求用户输入名词,但由于某种原因我收到该错误所以这是我的代码:
//Author:
//
//
//
//
//
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using std::string;
using namespace std;
string getWord(ifstream &Infile, char &ch);
void getFilename(ifstream &Infile);
void getOutfile(ofstream &Outfile);
void putWord(ostream &Outfile,string word, char space);
string replacement(string word,std::string);
void printprompt(int i);
int main()
{
ifstream filename;
ofstream outfile;
string file;
string word;
char ch;
getFilename(filename);
getOutfile(outfile);
while(!filename.eof())
{
file =getWord(filename,ch);
putWord(outfile,file,ch);
}
filename.close();
outfile.close();
return 0;
}
string getWord(ifstream &Infile, char &ch)
{
string wrd = "";
Infile.get(ch);
while(ch !=' '&& ch != 'n' && !Infile.eof())
{
wrd.append(1,ch);
Infile.get(ch);
}
return wrd;
}
void getFilename(ifstream &Infile)
{
string filename;
cout<<"File for input: ";
cin >>filename;
Infile.open(filename.c_str());
if (Infile.fail())
{
cout << "Cannot open"<<filename<<endl;
exit(0);
}
}
void getOutfile(ofstream &Outfile)
{
string filename;
cout<<"file for output: ";
cin>>filename;
Outfile.open(filename.c_str());
if(Outfile.fail())
{
cout<< "Cannot open"<<Outfile<<endl;
exit(0);
}
}
void putWord(ofstream &Outfile,string word, char c)
{
Outfile << word;
if (c =='n')
Outfile<<endl;
else
Outfile<<" ";
}
string replacement(string word)
{
string key[5]={"blank-N", "blank-A","blank-V","blank-P","blank-D"};
for(int i =0; i<5;i++)
{
if(word.compare(key[i]))
{
printprompt(i);
cin>>word;
}
return word;
}
}
void printprompt(int i)
{
switch(i)
{
case 0:
cout<<"Please enter a noun";
break;
case 1:
cout<<"Please enter an adjective";
break;
case 2:
cout<<"Please enter a verb";
break;
case 3:
cout<<"Please enter a place";
break;
default:;
}
}
我似乎收到此错误:
Undefined first referenced
symbol in file
putWord(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, char)/var/tmp//ccdj0m4f.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
我不知道如何防止此错误发生。
编译器告诉您定义与实现不匹配。 在这种情况下,参数类型不匹配。
您的定义:
void putWord(ostream &Outfile,string word, char space);
您的实施:
void putWord(ofstream &Outfile,string word, char c) { /* etc */ }
由于 ostream 和 ofstream 是不同的类型,因此无法编译。
相关文章:
- vscode g++链路故障:体系结构x86_64的未定义符号
- 在 Mac 上使用 CMAKE 将 FFTW 和 FFTWPP 链接到项目中时未定义的符号
- 在C++中使用内联方法时出现未定义的符号错误
- 引擎节点:未定义的符号:_ZTV6Config
- 未定义的引用,尽管存在符号,但 std::experimental::可选
- 体系结构x86_64的未定义符号:std:terminate(),typeinfo,运算符delete[],运算符new
- Windows 链接器是否使用 LoadLibrary 解析 DLL 中未定义的符号?
- C++ 中 dlsym 的未定义符号
- MacOS 上的 Xcode 11 项目不在一个函数中使用 sin 和 cos:未定义的符号"___sincosf_stret"
- 仅在 MacOS 上析构函数的未定义符号
- OSX clang++:用于 cpp 文件中显式实例化模板的体系结构x86_64的未定义符号
- PCL 出错:体系结构x86_64 @pcl的未定义符号
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 从 C++ 到 Python 的 SWIG:未定义的符号导入问题
- 使用静态变量未解析的外部符号/未定义的引用时出错
- 无法导出mexfunction:符号未定义的collect.exe2.exe:错误:ld返回1退出状态
- 模板生成错误:体系结构的符号未定义 x86_64:
- 继承-符号未定义
- __next_prime符号未定义
- 错误:对cv:: videoccapture::~ videoccapture()的引用符号未定义,命令行中缺少DSO