【发布时间】:2015-09-28 06:21:32
【问题描述】:
我有以下头文件(Student.h)
#ifndef STUDENT_H_
#define STUDENT_H_
#include <string>
using namespace std;
class Student {
int id;
string name;
string school;
public:
/*
* Default ctor
*/
Student();
/*
* Overloaded ctor
*/
Student(int id, string name, string school);
/*
* Compares two students by ID for equality
*/
bool operator == (const Student& other) const;
/*
* Compares two students by ID for ordering
*/
bool operator < (const Student& other) const;
};
#endif /* STUDENT_H_ */
这是我的实现(在另一个名为 Student.cpp 的文件中):
#include "Student.h"
#include <string>
using namespace std;
bool Student::operator==(const Student& other) const{
return(Student::id == other.id);
}
bool Student::operator<(const Student& other) const{
return(Student::id < other.id);
}
最后是一个引用这两个文件的 main.cpp 文件:
#include "Student.h"
#include "Student.cpp"
#include <iostream>
using namespace std;
int main() {
bool equal;
Student s1(111, "Jeff", "Rockywood High");
Student s2(100, "Bobby", "Carmel High");
equal = (s1==s2);
cout << equal;
}
我从 xcode 收到一个错误,告诉我:
架构 x86_64 的未定义符号:
Student::Student(int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
【问题讨论】:
-
Student(int id, string name, string school);的定义在哪里? -
在头文件中...
-
这是一个声明,定义在哪里?
-
所以你是说
class Student { int id; string name; string school; ...}不能作为定义?这对我来说是个新闻…… -
除非您没有编写此代码,否则您已经知道成员函数的声明和定义之间的区别,您已经提供了
operator==和operator<的定义,其中构造函数的定义?
标签: c++ xcode operator-overloading