C++ 错误 : Undefined Symbol

标签 c++ linker-errors

我的代码的重点是能够在文本中的某处输入带有空白-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 是不同的类型,因此无法编译。

关于C++ 错误 : Undefined Symbol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27007820/

相关文章:

C++ 嵌入式系统摩尔斯电码

用C计算复数的abs值

ios - 无法在 iOS 项目中使用库,该库在具有相同设置的另一个项目中工作

c - 不知道为什么我会收到链接错误

c++ - 为什么在头文件中声明并在文件中定义会出现多重定义错误?

c++ - mysql_stmt_execute 出现奇怪的错误

c++ - 在 RAD Studio 2007 的 Watch 中评估表达式

c++ - C++ 标准库平台是否独立?

c++ - 在映射 C++ 中存储对象引用

c++ - 带cmake的 Armadillo 链接器错误