简体   繁体   English

错误:使用未声明的标识符“ T”

[英]error: use of undeclared identifier 'T'

I am new to using templates with structures. 我是刚开始使用带有结构的模板。 My main goal is to have auto variable inside structure. 我的主要目标是在内部结构中具有自动变量。

More importantly, I am using a library function of Funct.Methd(const auto, &Handler). 更重要的是,我正在使用Funct.Methd(const auto,&Handler)的库函数。 Here as you can see, in the place of the first argument, I would like to pass (based on some criteria) any const auto variable that is created by the user. 如您所见,在这里,我想代替(基于某些条件)由用户创建的任何const auto变量。 I am expecting to have more than 20 variables, based on some criteria any 5 would be used. 我期望有20多个变量,根据某些标准,将使用5个变量。

After doing some research, I found that I can use template for this purpose. 经过研究后,我发现可以将模板用于此目的。 Please find the sample code below: 请在下面找到示例代码:

#include <iostream>
#include <string>

template<typename T>
struct FTmr {
    T query;
};

int main()
{
    FTmr<T> df;    
}

I am getting an error use of undeclared identifier 'T' when using the template with structures. 将模板与结构一起use of undeclared identifier 'T'我得到use of undeclared identifier 'T'的错误use of undeclared identifier 'T' First I need to resolve the above error and then I wanted to use const auto. 首先,我需要解决以上错误,然后再使用const auto。

I was hoping to instantiate FTmr with df name and then access query variable. 我希望用df实例化FTmr,然后访问查询变量。

df.query=<<const auto value>>

I would appreciate any help. 我将不胜感激任何帮助。

All variables in C++ have fixed types from the moment they are declared. 从声明起,C ++中的所有变量都具有固定类型。

Templates are not types. 模板不是类型。 Templates are instructions on how to make a type. 模板是有关如何进行键入的说明。

A const auto variable has a type determined by what you assign to it: const auto变量的类型取决于您为其分配的内容:

const auto x = foo();

the type of x is fixed, it is deduced by the fixed return type of foo() . x的类型是固定的,它由foo()的固定返回类型推导得出。 The language just finds it for you. 语言就是为您找到的。

A member of a struct is a variable. 结构的成员是变量。 It must have its type determined within the struct the moment it is declared. 在声明它时,必须在struct中确定其类型。 You cannot defer its type until it is used. 您不能推迟使用它的类型。

struct Foo {
  static const auto x = 3;
};

that works because x 's type can be determined at the spot where x is declared. 之所以有效,是因为可以在声明x的位置确定x的类型。

In short, const auto doesn't work like that. 简而言之, const auto不能那样工作。

It may be possible to do what you really want to do, but it might be hard , and you probably lack the vocabulary to describe what it is you really want. 可能可以做您真正想做的事情,但这可能很难 ,并且您可能缺乏词汇表来描述您真正想要的是什么。 My advice would be to not fight against the language and go with a fixed type. 我的建议是不要与语言抗争,而要使用固定类型。

What is T in main? T是什么? You have to define it before using it. 您必须先定义它,然后再使用它。 The previous T is scoped to the templated struct above. 前一个T的作用域为上面的templated struct Also you cannot use that way; 同样,您不能使用这种方式。 You have to instantiate the templated-struct this way: 您必须以这种方式实例化templated结构:

FTmr<int> df;
FTmr<char> df;
// ...

T is the templated parameter which means as if passing a variable to a function it means passing the type instead. T是模板化参数,这意味着好像将变量传递给函数一样,它意味着传递type So the compile will create an instance of the struct with the type passed int. 因此,编译将创建一个类型为int的结构实例。

  • Functions take variables as parameters this way: 函数以这种方式将变量作为参数:

     void foo(int x, char y, double z, struct foo& the foo, long* pBar, ...); 
  • Templates take types instead as parameters: 模板将types作为参数:

     template< class T> void Power(T& x); template < typename U, typename V> class Baz{ U _uval; V _vVal; }; 

So when instantiating the templated class / function the compiler will create an instance with that type: 因此,当实例化模板化的类/函数时,编译器将创建具有该类型的实例:

    Baz<int, std::string> bistr;

An instance created by the compiler: 编译器创建的实例:

class Baz{
    int _uval;
   std:: string _vVal;
};
  • Keep in mind that at compile time there is no template or type T but instead a specific version of the class is created. 请记住,在编译时没有模板或类型T,而是创建了该类的特定版本。

tl;dr: Templates don't read your mind. tl; dr:模板不会引起您的注意。 You still have to tell them what to do. 您仍然必须告诉他们该怎么做。

const auto is not a "variable type". const auto不是“变量类型”。 auto is a keyword that enables the deduction of a type from some information that you give it. auto是一个关键字,可以从您提供的某些信息中推断出类型。 For example, it knows that 5 is an int (because them's the rules), so auto x = 5; 例如,它知道5是一个int (因为它们是规则),所以auto x = 5; is possible. 是可能的。

In this case, you simply did not give it any information. 在这种情况下,您只是没有提供任何信息。 How do you think it should know what to use for T ? 您如何看待它应该为T使用什么? We don't even know! 我们甚至都不知道! Do you know? 你知道吗? What should query be? query应该是什么? What should it do? 应该怎么办? Think it through, then let your computer know what you've decided. 仔细考虑,然后让您的计算机知道您的决定。

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

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