不太明白,为何这程序加上&就能编译通过,不加&就不能通过

C++语言 码拜 8年前 (2016-02-01) 717次浏览
头文件person.h如下:
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
#include <string>
class Person{
private:
static const int LIMIT = 25;
std::string lname;
char fname[LIMIT];
public:
Person(const std::string &ln,const char * fn);
void show() const;
void FormalShow() const;
};
#endif
函数文件person.cpp如下:
#include “person.h”
#include <iostream>
#include <cstring>
#include <string>
using std::cout;
using std::endl;
void Person::show() const
{
cout<<“His name is “;
cout<<fname<<” . “<<lname<<endl;
}
void Person::FormalShow() const
{
cout<<“his name is “;
cout<<lname<<” . “<<fname<<endl;
}
Person::Person(const std::string &ln,const char * fn)
{
lname=ln;
strcpy(fname,fn);
}
主函数文件如下:
#include “person.h”
#include <iostream>
int main()
{
Person Ann(“Jack”,”Chen”);
Ann.show();
Ann.FormalShow();
return 0;
}
其中的构造函数Person(const std::string &ln,const char * fn)就能编译通过,假如是去掉其中的&符,就不能通过,象这样
Person(const std::string ln,const char * fn),就不能编译通过了。
错误信息:main.cpp:(.text+0x9b): undefined reference to `Person::Person(std::string const&, char const*)””
main.cpp:(.text+0x121): undefined reference to `Person::Person(std::string const&, char const*)””
[Error] ld returned 1 exit status
recipe for target “”person.exe”” failed
搞不懂呀,请高手赐教!
解决方案:10分
原因是你只删除了头文件和cpp文件中的一处
解决方案:10分
意思你头文件声明和实现文件里的实现,函数的原型不一致,提示你cpp里面的那个未定义

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明不太明白,为何这程序加上&就能编译通过,不加&就不能通过
喜欢 (0)
[1034331897@qq.com]
分享 (0)