C++引入.h报错

初学c++想封装一个类引入.h报错

Student.h文件

#ifndef C_STUDENT_H
#define C_STUDENT_H
class Student{
    public:
        void data();
};
#endif //C_STUDENT_H

Student.cpp文件

#include "Student.h"

void Student::data()
{
    printf("hello");
}

main.cpp文件

#include <iostream>
#include "Student.h"
int main() {
    Student student;
    student.data();
    return 0;
}

报错为

undefined symbols for architecture x86_64: "Student::data()",
referenced from:

 _main in main-c01688.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to

see invocation)

阅读 2.9k
2 个回答

先把代码排版好

根据你的错误信息,你的编译的命令行应该有误,顺便一道贴出来看看

个人猜测,你是没有加入Student.cpp的编译:

cc main.cpp Student.cpp -o a.out

先把代码排版好

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题