简体   繁体   English

C ++模板语法

[英]C++ template syntax

I don't understand what is wrong with this code. 我不明白这段代码有什么问题。

gcc reports "Client.h:29: error: expected template-name before '<' token" gcc报告“ Client.h:29:错误:'<'标记之前的预期模板名称”

As far as I'm aware I'm followed the template syntax correctly, but it could be that the error message is confusing me and is not the problem 据我所知,我遵循正确的模板语法,但它可能是错误消息是混淆我,是不是问题

client.h 客户端

class Client : public BaseDll<DllClient> [line 29]
{
..snip..
};

basedll.h 基于h

template<typename T>
class BaseDll : public Base
{
public:
    ..snip..

private:
  T* _dll;
};
class Base{
};
template<typename T>
class BaseDll:public Base{
public:
private:
T* _dll;
};
class DllClient{
};
class Clien:public BaseDll<DllClient>
{
};

this compiled for me without problems, so I don't think the problem lies within what you posted. 这对我来说没有问题,所以我认为问题不在您发布的内容之内。 My best bet would be that you made a syntax error in client.h, maybe something as simple as forgetting a semicolon after another class definition or some macro that's messing with your code 最好的选择是,您在client.h中犯了语法错误,可能就像在另一个类定义之后遗忘了分号或某个使您的代码混乱的宏一样简单

I'm so sorry everyone, a school-boy error has been made, BaseDll is declared in another namespace. 抱歉,每个人都犯了一个男生错误,在另一个命名空间中声明了BaseDll。 As soon as I added the namespace qualifier, the problem has gone. 一旦添加了名称空间限定符,问题就消失了。

也许这只是一个简单的问题:您是否在client.h包含了basedll.h

to cover the basics: 涵盖基础知识:

does client-h #include basedll.h? client-h #include basedll.h吗? do they user different include guards? 他们使用不同的包括警卫人员吗?

Further troubleshooting: does it work with a non-template base class? 进一步的故障排除:它适用于非模板基类吗? does it work then you typedef the template instaltiation: 它是否起作用,然后您键入def模板实例:

typedef BaseDll<DllClient> tClientBase;
class Client : public tClientBase { ... }

[edit] OK, next: [编辑]好,接下来:

if you put the following two lines directly under the BaseDll declaration: 如果将以下两行直接放在BaseDll声明下:

template <typename T>
class BaseDll
{ ... 
};
class DummyFoo;
typedef BaseDll<DummyFoo> tDummyFoo;

I think you snipped away the problem. 我认为您已经解决了这个问题。 Are you including something to define 'DllClient' ? 您是否包含定义“ DllClient”的内容?

A possible cause of this is that there is an inter-dependency between the different header files: 可能的原因是不同的头文件之间存在相互依赖性:

// client.h
#ifndef CLIENT
#define CLIENT

#include "base.h"

// ... 

class Client : public BaseDll<DllClient>
{
  // ..snip..
};

#endif


// base.h
#ifndef BASE
#define BASE

#include "client.h"

template<typename T>
class BaseDll : public Base
{
public:
  // ..snip..

private:
  T* _dll;
};

#endif

Now imagine we're parsing 'base.cpp' then the preprocessor will do the following: 现在假设我们正在解析“ base.cpp”,那么预处理器将执行以下操作:

#include "base.h"
#ifndef BASE        <--- BASE unset, keep going
#define BASE
#include "client.h"
#ifndef CLIENT
#define CLIENT
#include "base.h" 
#ifndef BASE        <--- BASE set, skip base.h, return to client.h
class client
 : public BaseDll<DllClient>  <-- ERROR, BaseDll not defined.

If this is the problem, then you potentially can get around it by forward declaring the base template in client.h: 如果这是问题所在,那么您可以通过在client.h中向前声明基本模板来解决该问题:

// client.h
#ifndef CLIENT
#define CLIENT

// #include "base.h"    <-- remove include
template <typename DLL_CLIENT>
class BaseDll;

// ... 

class Client : public BaseDll<DllClient>
{
  // ..snip..
};

#endif

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

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