C ++ Apple Mach-O链接器错误/符号“::”未找到架构

时间:2015-09-28 06:21:33

标签: c++ xcode operator-overloading

我有以下头文件(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

1 个答案:

答案 0 :(得分:0)

通过将#include "Student.cpp"放在Student.cpp文件中,我已经将同一个文件包含两次,但没有意识到。因此,Student::Student被定义了两次......因此重复的符号错误。