【问题标题】:C++ undefined reference to `Class::Ctor'C++ 未定义对 `Class::Ctor' 的引用
【发布时间】:2016-03-31 11:39:15
【问题描述】:

我有这个错误消息

在函数'int main()'中:| 未使用的变量'Bobi' 在函数'main'中:|

对`Student::Student()'的未定义引用|

||=== 构建失败:1 个错误,1 个警告(0 分钟,1 秒)===|

你能帮我解决这个问题并检查我的方法声明

这是我的 ma​​in.cpp

    #include <iostream>
#include "Student.h"
using namespace std;

int main()
{
    Student *Bobi= new Student();

    return 0;
}

这是我的标题

    #ifndef STUDENT_H
#define STUDENT_H
#include <string>

class Student
{
    public:
        Student();
        virtual ~Student();
        std::string getName()
        {
            return this->name;
        }
        void setName(std::string name)
        {
            this->name=name;
        }
        int getNumber()
        {
            return this->number;
        }
        void setNumber(int number)
        {
            this->number=number;
        }
        std::string getSurname()
        {
            return this->surname;
        }
        void setSurname(std::string surname)
        {
            this->surname=surname;
        }
        double setDiploma1(double diploma1)
        {
            return this->diploma1;
        }
        void getDiploma1()
        {
            this->diploma1=diploma1;
        }
        double setDiploma2(double diploma2)
        {
            return this->diploma2;
        }
        void getDiploma2()
        {
            this->diploma2=diploma2;
        }
        double setIzpit1(double izpit1)
        {
            return this->izpit1;
        }
        void getIzpit1()
        {
            this->izpit1=izpit1;
        }
        double setIzpit2(double izpit2)
        {
            return this->izpit2;
        }
        void getIzpit2()
        {
            this->izpit1=izpit2;
        }
        double Bal(double setDiploma1,double setDiploma2,double setIzpit1,double setIzpit2)
        {
            double bal=setDiploma1+setDiploma2+setIzpit1+setIzpit2;
            return bal;
        }
    protected:
    private:
        int number;
        std::string name;
        std::string surname;
        double diploma1;
        double diploma2;
        double izpit1;
        double izpit2;
};

#endif // STUDENT_H

Student.cpp

#include "Student.h"
#include <iostream>
using namespace std;
Student::Student()
{
}
Student::~Student()
{
}

【问题讨论】:

  • 你用来编译它的 makefile 或命令是什么?
  • 你如何构建你的项目?您确定使用Student.cpp 源文件构建吗?它必须在 IDE 的 project 中,而不仅仅是在磁盘上的目录中。
  • 它在我的 IDE 项目中
  • 我构建它然后运行它。
  • 在尝试构建时不要谈论您当前的错误,但是为什么您的set 函数没有任何参数?您将班级成员设置为自身...有点奇怪,不是吗?

标签: c++ class oop header


【解决方案1】:

完全删除 Student.cpp 文件,然后在 main 中使用一些 set get 运行。立即运行

它应该工作。看看student.cpp应该头bjt你甚至不需要它只是在你有.bc虚拟使用的头文件的析构函数中定义构造函数是错误的

【讨论】:

    猜你喜欢
    • 2011-03-31
    • 1970-01-01
    • 2013-01-19
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多