1个头文件和两个源文件的问题
下边的三个文件来自c++ primer plus ,我分别写在三个文件中,但是之后点 “生成解决方案”就总是出现一下的类似命令,请问大神怎么解决?是头文件哪里出了什么问题?无法解析的外部符号 "void __cdecl callme2(class StringBad)" (?callme2@@YAXVStringBad@@@Z),该符号在函数 _main 中被引用
1>E:\Clang编译器\PROJECTS\20170830\Debug\20170830.exe : fatal error LNK1120: 1 个无法解析的外部命令
这是头文件 string.h
#include<iostream>
#ifndef STRINGBAD_H_
#define STRINGBAD_H_
class StringBad
{
private:
char* str;
int len;
static int num_string;
public:
StringBad(const char* s);
StringBad();
~StringBad(); //析构函数没有参数
friend std::ostream& operator<<(std::ostream &os,const StringBad &st);
};
#endif
这是源文件1: stringbad.cpp
#include <cstring>
#include "stringbad.h"
using namespace std;
int StringBad::num_string=0;
StringBad::StringBad(const char*s)
{
len=std::strlen(s);
str=new char[len+1];
strcpy_s(str,len+1,s);
num_string++;
std::cout<<num_string<<endl;
}
StringBad::StringBad()
{
len=4;
str=new char[4];
strcpy_s(str,4,"C++");
num_string++;
cout<<num_string<<":\""<<str<<"\"default object created\n";
}
StringBad::~StringBad()
{
cout<<"\""<<str<<"\" object deletd, ";
--num_string;
cout<<num_string<<" left\n";
delete[]str;
}
ostream& operator<<(ostream &os,const StringBad&st)
{
os<<st.str;
return os;
}
这是源文件2: vegnews.cpp
#include<iostream>
using namespace std;
#include "stringbad.h"
void callme1(StringBad &);
void callme2(StringBad);
int main()
{
{
cout<<"starting an inner block.\n";
StringBad headline1("celery stalks an midnight");
StringBad headline2("Lettuce Prey");
StringBad sports("Spinach Leaves bowl for dollars");
cout<<headline1<<endl;
cout<<headline2<<endl;
cout<<sports<<endl;
callme1(headline1);
cout<<headline1<<endl;
callme2(headline1);
cout<<headline2<<endl;
cout<<"Initialize one object to another:\n";
StringBad sailor=sports;
cout<<"sailors:"<<sailor<<endl;
cout<<"assign one object to another:\n";
StringBad knot;
cout<<knot<<endl;
cout<<"exiting the block.\n";
}
cout<<"end of the main()\n";
return 0;
}
void callme1(StringBad&rsd)
{
cout<<rsd<<endl;
}
void callme2(StringBad&sb)
{
cout<<sb<<endl;
}