c++ - Constructor of structure error(调用类的constructor denconstructor,填充结构)

标签 c++ data-structures constructor

我有以下结构:

template <class T>
struct Array{
    int lenght;
    T * M;

    Array( int size ) : lenght(size), M(new T[size])
    {
    }

    ~Array()
    {
       delete[] M;
    }
};

和类(将填充结构的对象):

class Student{

private:
int ID;
int group;
char name[];
 public:

     Student();
     ~Student();

    void setStudent(int,int,char){

    }

    char getName(){
        return *name;
    }

    void getGroup(){

    }

    void getID(){

    }

};

现在当我想初始化 Array 类型时,我在 Main.cpp 中得到以下内容:

#include <iostream>
#include "Domain.h"
#include "Student.h"
//#include ""

using namespace std;

int main(){
    cout<<"start:"<<endl<<endl;

    Array <Student> DB(50);
    Array <Student> BU(50);


    return 0;
}

错误:

g++ -o Lab6-8.exe UI.o Repository.o Main.o Domain.o Controller.o
Main.o: In function `Array':
D:\c++\Begin\Lab6-8\Debug/..//Domain.h:16: undefined reference to `Student::Student()'
D:\c++\Begin\Lab6-8\Debug/..//Domain.h:16: undefined reference to `Student::~Student()'
Main.o: In function `~Array':
D:\c++\Begin\Lab6-8\Debug/..//Domain.h:21: undefined reference to `Student::~Student()'

知道为什么吗?

最佳答案

当你写的时候:

class Student
{
public:
   Student();
   ~Student();
};

显式声明类构造函数和析构函数,因此编译器没有为您定义它们——您需要提供它们的定义(实现)。在一般情况下,这可以完成工作:

class Student
{
public:
   Student(){};
   ~Student(){};
};

关于c++ - Constructor of structure error(调用类的constructor denconstructor,填充结构),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10386787/

相关文章:

c++ - 在不使用 `protected` 的情况下,子类如何有效地使用基类中定义的变量

c++ - 以下片段的时间复杂度是 O(n^2) 吗?

mysql - 关系数据库结构 - 最佳实践?

c++ - 使用包含静态成员变量的类的 vector

c++ - 十六进制、八进制、二进制转十进制(C++)

c++ - Int 追加到 char,行为不符合我的预期

java - 如何按数字对列表进行排序,如果重复则按字符串排序?

algorithm - 处理棋盘对称性

java - 尝试将 JTextField 转换为 Integer 时出现 NumberFormatException

c++ - 继承公共(public)/ protected /私有(private)构造函数