简体   繁体   English

类模板实例化错误:未在此范围内声明类型

[英]Class template instantiation error: type not declared in this scope

So I am writing a particle accelerator code for a c++ class, and Im having trouble with the initial class set up Im being asked to use. 所以我正在为一个c ++类编写一个粒子加速器代码,并且我在初始类设置时遇到了麻烦。我被要求使用。 The professor wants us to use templates for the class, but when I try to implement them, I receive a number of errors. 教授希望我们为课程使用模板,但是当我尝试实现它们时,我收到了许多错误。 For instance: 例如:

#include <iostream>
#include <cmath>

using namespace std;
template <class Type>
class ThreeVector {
private:
    Type mx;
    Type my;
    Type mz;
public:
    ThreeVector();
    ThreeVector(Type x=0, Type y=0, Type z=0);


};

ThreeVector<Type>::ThreeVector() {
    Type mx=0;
    Type my=0;
    Type mz=0;
}
ThreeVector<T>::ThreeVector(Type x, Type y, Type z) {
    mx=x;
    my=y;
    mz=z;
}

is part of my header file (the class is required to be in a header file). 是我的头文件的一部分(该类必须在头文件中)。 When I run my program (can provide if needed), it gives me the following errors: 当我运行我的程序(如果需要可以提供)时,它会给我以下错误:

ThreeVector.h:30:13: error: 'Type' was not declared in this scope ThreeVector.h:30:13:错误:未在此范围内声明“类型”

ThreeVector.h:30:17: error: template argument 1 is invalid ThreeVector.h:30:17:错误:模板参数1无效

ThreeVector.h: In function 'int ThreeVector()': ThreeVector.h:30:32: error: 'int ThreeVector()' redeclared as different kind of symbol ThreeVector.h:在函数'int ThreeVector()'中:ThreeVector.h:30:32:错误:'int ThreeVector()'重新声明为不同类型的符号

ThreeVector.h:6:7: note: previous declaration 'template class ThreeVector' ThreeVector.h:6:7:注意:上一个声明'模板类ThreeVector'

ThreeVector.h:31:2: error: 'Type' was not declared in this scope ThreeVector.h:31:2:错误:未在此范围内声明“类型”

ThreeVector.h:32:7: error: expected ';' ThreeVector.h:32:7:错误:预期';' before 'my' 在'我'之前

ThreeVector.h:33:7: error: expected ';' ThreeVector.h:33:7:错误:预期';' before 'mz' 在'mz'之前

ThreeVector.h: At global scope: ThreeVector.h:35:13: error: 'Type' was not declared in this scope ThreeVector.h:在全局范围:ThreeVector.h:35:13:错误:未在此范围内声明“类型”

These problems did not exist before I started using the template, ie, if I explicitly define all variable types, my class works fine. 在我开始使用模板之前,这些问题不存在,即,如果我明确定义了所有变量类型,我的类工作正常。 So, Im not really sure what is wrong with my template definition? 所以,我不确定我的模板定义有什么问题? If anyone could help, Id be really appreciative. 如果有人可以提供帮助,我真的很感激。

Thanks! 谢谢!

You have to declare template arguments for methods that are part of a template class and yet are defined outside of the class definition. 您必须为属于模板类的方法声明模板参数,但这些方法是在类定义之外定义的。 So to make your function definitions you have to do this: 因此,要制作函数定义,必须执行以下操作:

template<class Type>
ThreeVector<Type>::ThreeVector() {
    mx=0;
    my=0;
    mz=0;
}

template<class Type>
ThreeVector<T>::ThreeVector(Type x, Type y, Type z) {
    mx=x;
    my=y;
    mz=z;
}

This is because the templated type is not available to the method declarations when they are outside of the class definition. 这是因为模板类型在类定义之外时不可用于方法声明。 To fix this you have to add a template< ... > with the same arguments as for the original class. 要解决此问题,您必须添加一个template< ... > ,其参数与原始类相同。

Also, you should use initializer lists to initialize members. 此外,您应该使用初始化列表来初始化成员。 That would make your constructors look like this: 这将使您的构造函数看起来像这样:

template<class Type>
ThreeVector<Type>::ThreeVector() : 
    mx(0),
    my(0),
    mz(0)
{

}
  1. You forgot to add 你忘了添加

     template <typename Type> 

    Before the function definition. 在功能定义之前。

  2. Remove Type from the body of the function. 从函数体中删除Type When you add Type , you are declaring three function variables that are not related to the class members. 添加Type ,您将声明三个与类成员无关的函数变量。

template <typename Type>
ThreeVector<Type>::ThreeVector() {
    mx=0;
    my=0;
    mz=0;
}

You can make it better by initializing the member using the initializer list syntax: 您可以通过使用初始化列表语法初始化成员来使其更好:

template <typename Type>
ThreeVector<Type>::ThreeVector() : mx(0), my(0), mz(0) { }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM