【问题标题】:C++ Apple Mach-O Linker Error/Symbols "::" Not Found for ArchitectureC++ Apple Mach-O 链接器错误/符号“::”未找到架构
【发布时间】: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&lt; 的定义,其中构造函数的定义?

标签: c++ xcode operator-overloading


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    相关资源
    最近更新 更多