获取以下 c++ 代码的链接器错误
Getting the linker error for following c++ code
我最近开始学习面向对象编程。对于以下代码,我收到链接器错误!
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class person{
public:
string name;
person();
person(string n){
name = n ;
}
void setName(string k){
name = k;
}
string getName(){
return name;
}
};
class student : public person {
public:
string major;
void setMajor(string m){
major = m;
}
string getMajor(){
return major;
}
};
class faculty : public person{
public:
string department;
faculty(string dept){
department = dept;
}
void setDepartment(string depart){
department = depart;
}
string getDepartment(){
return department;
}
};
int main() {
student s;
s.setName("james");
s.setMajor("computer science");
string p = s.getName();
string p2 = s.getMajor();
cout << "student name and mjor is :" << p << p2 << endl;
faculty f("nanotech");
f.setName("chris");
f.setDepartment("electrical");
string f1 = f.getName();
string f2 = f.getDepartment();
cout << "facult name and department :" << f1 << f2 << endl;
return 0;
}
当我尝试编译代码时,我收到以下错误。
/tmp/ccYHu2de.o: In function `faculty::faculty(std::string)':
person.cpp:(.text._ZN7facultyC2ESs[_ZN7facultyC5ESs]+0x19): undefined reference to `person::person()'
/tmp/ccYHu2de.o: In function `student::student()':
person.cpp:(.text._ZN7studentC2Ev[_ZN7studentC5Ev]+0x15): undefined reference to `person::person()'
collect2: error: ld returned 1 exit status
在你的类人中,你有一个声明但未定义的构造函数。编译器允许您编译类,但链接器随后会查找实现(称为前向声明):
class person{
public:
string name;
person(); //constructor only declared
person(string n){
name = n ;
}
.....
};
问题是你从学生和教职员工那里继承,你使用默认构造函数,但你从未实现它。尝试添加实现,链接器错误将消失:
class person{
public:
string name;
person(){} //constructor implemented
person(string n){
name = n ;
}
.....
};
问题是你需要实现 person 类的构造函数,试试这段代码: http://ideone.com/NfXP7R
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class person{
public:
string name;
person() {
}
person(string n){
name = n ;
}
void setName(string k){
name = k;
}
string getName(){
return name;
}
};
class student : public person {
public:
string major;
void setMajor(string m){
major = m;
}
string getMajor(){
return major;
}
};
class faculty : public person{
public:
string department;
faculty(string dept){
department = dept;
}
void setDepartment(string depart){
department = depart;
}
string getDepartment(){
return department;
}
};
int main() {
student s;
s.setName("james");
s.setMajor("computer science");
string p = s.getName();
string p2 = s.getMajor();
cout << "student name and mjor is :" << p << p2 << endl;
faculty f("nanotech");
f.setName("chris");
f.setDepartment("electrical");
string f1 = f.getName();
string f2 = f.getDepartment();
cout << "facult name and department :" << f1 << f2 << endl;
return 0;
}
相关文章:
- 静态数据成员的问题-修复链接错误会导致编译器错误
- Visual Studio mkl_link_tool.exe链接错误
- C++ 实现模板单例类时出现链接错误
- 使用类模板的方法链接错误
- MySQL C++连接器链接错误
- VS 2015 链接错误 无法构建依赖于 libcurl 的项目
- 运行 C++ 单元测试时LNK2005链接错误
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 链接错误,包括我创建的相同头文件 - C++
- 在Embarcadero C++ Builder中生成的DLL未解决的外部链接错误
- 使用标头保护的多个定义链接错误
- 链接错误:未定义对 stdscr 和 wgetch 的引用
- 使用 Vivek 的 Vcam / 捕获源过滤器构建/链接错误
- 升压program_options中的链接错误
- 使用 g++ 预处理器进行替换会导致链接错误
- 在调试配置中编译工作正常,但发布会给出链接错误
- 相邻矩阵设置链接错误
- 包含常量变量并包含在多个文件中的标头的链接错误
- C++链接错误,我理解但无法解决
- 是什么导致macOS Mojave上的GoogleTest链接错误