问题描述
#include <iostream>
using namespace std;
template<class T>
class people{
public:
virtual void insert(T item)=0;
virtual T show(T info)=0;
};
template<class T>
class name
{
private:
T fname;
T lname;
public:
name(T first, T last);
// bool operator== (name & p1, name &p2)
};
template <class T>
name<T>::name(T first, T last){
fname = first;
lname = last;
}
template <class T>
class person : public people<T>
{
private:
T a[1];
int size;
public:
person();
virtual void insert(T info);
virtual T show();
};
template<class T>
person<T>::person(){
size = 0;
}
template<class T>
void person<T>::insert(T info){
a[0] =info;
}
template<class T>
T person<T>::show(){
return a[0];
}
int main(){
string first("Julia"), last("Robert");
name<string> temp(first,last);
people<name<string>>* aPerson = new person<name<string>>();
aPerson-> insert(temp);
aPerson->show();
return 0;
}
这是我不断收到错误,我无法找出真正的问题是:
These are the errors I keep getting and I can't pinpoint what really is the problem:
TEST.CPP:52:错误:在模板参数1型/价值不匹配
对于模板类人TEST.CPP参数表:52:
错误:预期的类型,有'名'TEST.CPP:52:错误:无效的类型
在声明之前'='令牌TEST.CPP:52:错误:预期
前人TEST.CPP类型说明符:52:错误:预期','或';'
前人TEST.CPP:53:错误:要求在'成员'插入'*
aPerson,这是无类类型'诠释'TEST.CPP:54:错误:要求
在* aPerson,这是无类类型的成员'秀''诠释'
变化的线路52后:人> * aPerson =新的人>();
我得到新的一系列错误:
After changing line 52 to: people>* aPerson = new person>();I am getting new series of errors:
TEST.CPP:在函数'廉政的main()':
test.cpp: In function 'int main()':
TEST.CPP:52:错误:'aPerson在此范围未声明
test.cpp:52: error: 'aPerson' was not declared in this scope
TEST.CPP:52:错误:'*'不能出现在一个恒定的前pression
test.cpp:52: error: '*' cannot appear in a constant-expression
TEST.CPP:52:错误:'>>'应该是'>>'嵌套模板参数列表中
test.cpp:52: error: '>>' should be '> >' within a nested template argument list
TEST.CPP:52:错误:无法分配抽象类型的对象'的人,性病::分配器>>>
test.cpp:52: error: cannot allocate an object of abstract type 'person, std::allocator > > >'
TEST.CPP:28:注意:因为以下虚函数中纯的人,性病::分配器>>>
test.cpp:28: note: because the following virtual functions are pure within 'person, std::allocator > > >':
TEST.CPP:8:注:t人::展(T)与T =名称,性病::分配器>>]
test.cpp:8: note: T people::show(T) [with T = name, std::allocator > >]
TEST.CPP:52:错误:分配不能出现在一个恒定的-EX pression
test.cpp:52: error: an assignment cannot appear in a constant-expression
TEST.CPP:52:错误:'>>'应该是'>>'嵌套模板参数列表中
test.cpp:52: error: '>>' should be '> >' within a nested template argument list
TEST.CPP:52:错误:无法分配抽象类型的对象的人
的std :: char_traits,性病::分配器>>>
test.cpp:52: error: cannot allocate an object of abstract type 'person
std::char_traits, std::allocator > > >'
TEST.CPP:28:注意:由于类型'的人,性病::分配器>>>的纯虚函数
test.cpp:28: note: since type 'person, std::allocator > > >' has pure virtual functions
推荐答案
名称
是一个模板类,所以你必须指定模板:
name
is a templated class, so you must specify the template:
people<name<string>>* aPerson = new person<name<string>>();
这篇关于C ++类/参数在1模板参数列表值不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!