简体   繁体   中英

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). 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. I am expecting to have more than 20 variables, based on some criteria any 5 would be used.

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. First I need to resolve the above error and then I wanted to use const auto.

I was hoping to instantiate FTmr with df name and then access query variable.

df.query=<<const auto value>>

I would appreciate any help.

All variables in C++ have fixed types from the moment they are declared.

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 x = foo();

the type of x is fixed, it is deduced by the fixed return type of 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. 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.

In short, const auto doesn't work like that.

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? You have to define it before using it. The previous T is scoped to the templated struct above. Also you cannot use that way; You have to instantiate the templated-struct this way:

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. So the compile will create an instance of the struct with the type passed 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:

     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.

tl;dr: Templates don't read your mind. You still have to tell them what to do.

const auto is not a "variable type". auto is a keyword that enables the deduction of a type from some information that you give it. For example, it knows that 5 is an int (because them's the rules), so 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 ? We don't even know! Do you know? What should query be? What should it do? Think it through, then let your computer know what you've decided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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