a(10)"有初始值设定项但类型不完整”-6ren"> a(10)"有初始值设定项但类型不完整”- 这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Why can templates only be implemented in the header fi-6ren">
gpt4 book ai didi

c++ - 编译器抛出一个错误,指出 "Dict a(10)"有初始值设定项但类型不完整”

转载 作者:行者123 更新时间:2023-11-27 23:25:53 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Why can templates only be implemented in the header file?

类声明.h

template<typename anytype>
class List;

template<typename anytype>
class Dict;

类定义.cpp

#include "classdeclaration.h"
#include<iostream>

using std::cout;
using std::endl;

template<typename anytype>
class List{
private:
int size;
anytype * list;
public:
List(int a);
List(const List<anytype> &a);
int getSize();
~List();
};

template<typename anytype>
List<anytype>::List(int a){
size=a;
list=new anytype[size];
cout<<"List object initialized. The address of list is "<<list<<endl;
}

template<typename anytype>
int List<anytype>::getSize(){
return size;
}

template<typename anytype>
List<anytype>::List(const List<anytype> &a){
this.size=a.getSize();
this.list=new List<anytype>(size);
}

template<typename anytype>
List<anytype>::~List(){
delete[] list;
cout<<"List destructor called."<<endl;

}


template<typename anytype>
class Dict{
private:
int dicta;
List<anytype> *dict;
public:
Dict(int num);
Dict(const Dict<anytype>& a);
int getDicta();
~Dict();
};

template<typename anytype>
Dict<anytype>::Dict(int num){
dicta=num;
dict=new List<anytype>(dicta);
cout<<"Dict object initialized. The address of dict is "<<dict<<endl;
}

template<typename anytype>
int Dict<anytype>::getDicta(){
return dicta;
}

template<typename anytype>
Dict<anytype>::Dict(const Dict<anytype> & a){
this.dicta=a.getDicta;
dict=new Dict<anytype>(dicta);
}

template<typename anytype>
Dict<anytype>::~Dict(){
delete[] dict;
cout<<"Dict destructor called."<<endl;
}

测试.cpp

#include<iostream>
#include "classdeclaration.h" //A


int main()
{
using namespace std;
Dict<int> a(10);
cout<<"The address of Dict<int> a is "<<&a<<endl;
}

这里的问题是文件不会用命令“g++ test.cpp classdefinition.cpp -o e:\KC”编译,编译器抛出一个错误说 Dict<int> a(10)有初始值设定项但类型不完整。类型不完整是什么意思,我该如何解决?

另一个问题是,如果我用#include "classdefinition.cpp"替换 A 行的预处理器语句,文件将编译但无法运行。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com