English 中文(简体)
A. 在不同头盔安排有关代码的模板
原标题:Arranging template related code in different headers

The code below works as long as I keep it all in the "main.cpp" file.

//#include "Travel.h"
//#include "Obj.h"

// "Travel.h"
typedef int travel_t;

class Travel
{

public:

    static const travel_t AIR;
    static const travel_t WATER;
    static const travel_t LAND;

};

// "Travel.cpp"
// #ifndef TRAVEL_H
// #define TRAVEL_H
//
// #include "Travel.h"
const travel_t Travel::AIR = -2;
const travel_t Travel::WATER = -1;
const travel_t Travel::LAND = 0;
// #endif //TRAVEL_H

// "Obj.h"
// #ifndef OBJ_H
// #define OBJ_H
//
//#include "Travel.h"
template<typename T, travel_t travel>
class Obj
{
public:
    void foo(){};
};
// #endif //OBJ_H

// "main.cpp"
int main()
{
    Obj<int, Travel::AIR> objAirTravel;

    objAirTravel.foo();

    return 0;
}

However, as soon as I moved code to different headers and implementation files as indicated, it doesn t compile any more. :-( How can I fix that problem? What is the problem/rule behind it? This is the compiler error I get (using gcc):

main.cpp|45|error:  Travel::AIR  is not a valid template argument for type  int  because it is a non-constant expression|
main.cpp|45|error: invalid type in declaration before  ;  token|
main.cpp|47|error: request for member  foo  in  objAirTravel , which is of non-class type  int |
最佳回答

为了使用一个固定的模板论点,其价值必须放在目前的翻译单位。 当你将<代码>Travel:Air的定义移至不同的来源档案中时,其价值不再在main上提供给汇编者。

既然是惯犯,你可以宣布:

class Travel
{
public:
    static const travel_t AIR = -2;
    static const travel_t WATER = -1;
    static const travel_t LAND = 0;
};

现在,这些数值可用作包括本类定义的任何翻译单位的模板论据。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签