【问题标题】:c++ type/value mismatch at argument 1 in template parameter listc ++模板参数列表中参数1的类型/值不匹配
【发布时间】:2013-11-09 15:54:02
【问题描述】:
#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>* aPerson = new person();
    aPerson-> insert(temp);
    aPerson->show();
    return 0;
}

这些是我不断遇到的错误,我无法确定真正的问题是什么:

test.cpp:52: error: type/value mismatch at argument 1 in template parameter list for 'template<class T> class people'
test.cpp:52: error:   expected a type, got 'name'
test.cpp:52: error: invalid type in declaration before '=' token
test.cpp:52: error: expected type-specifier before 'person'
test.cpp:52: error: expected ',' or ';' before 'person'
test.cpp:53: error: request for member 'insert' in '* aPerson', which is of non-class type 'int'
test.cpp:54: error: request for member 'show' in '* aPerson', which is of non-class type 'int'

【问题讨论】:

  • 题外话,但你应该改掉在不需要时使用new 以及在需要时使用哑指针的习惯。否则,您最终会像示例中那样花费一生来调试内存泄漏,而不是编写有趣的代码。
  • @MikeSeymour 我是来学习的。如果我放弃使用 new,您能否提供一些替代方法?
  • 在这种情况下,一个简单的自动局部变量:people&lt;name&lt;string&gt;&gt; aPerson; 当您确实需要动态分配时(因为对象必须比创建它的函数寿命更长),您应该了解RAII 并且,特别是智能指针.
  • @MikeSeymour 如果我要使用人员> aPerson; aPerson 会有一个类型名称的数组吗?
  • 对不起,我没有正确阅读代码。你需要aPerson 成为具体类型person,而不是抽象people。不过仍然不需要new

标签: c++ templates arguments type-mismatch


【解决方案1】:

name是模板类,所以必须指定模板:

people<name<string>>* aPerson = new person<name<string>>();

【讨论】:

  • @user2972206 不客气!如果您将此问题标记为resolved,也会对其他人有所帮助。
  • 这段代码最终会出现编译器错误:'>>' 应该是 '> >' 在嵌套模板参数列表中
  • @John_West &gt;&gt; 支持 [stackoverflow.com/q/6695261/1969455](from C++11 on)。
  • @MatthäusBrandl 除了链接之外没有任何证据。
  • @John_West N3337 14.2.3: [..] 同样,第一个非嵌套的 >> 被视为两个连续但不同的 > 标记,[..]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
相关资源
最近更新 更多