C 中的继承和名称空间
Inheritance and Namespaces in c++
image.h
#include <iostream>
#include "Array.h"
using namespace math;
namespace imaging
{
class Image: public Array
{
public:
Image();
};
}
array.h
namespace math
{
template <typename T>
class Array
{
protected:
T * buffer;
unsigned int width, height;
public:
Array(unsigned int w, unsigned int h);
};
}
和array.cpp
#include <iostream>
using namespace std;
namespace math
{
Array::Array(unsigned int w, unsigned int h)
{
this->width = w;
this->height = h;
}
}
我有这些错误:image.h:12:2:错误: {
token
{
^在image.cpp中包含的文件中:1:0:image.h:12:2:错误: {
token之前的预期类名称
{
^array.cpp:8:2:错误:无效使用模板名称math::Array
没有参数列表 Array::Array(unsigned int w, unsigned int h)
为此有任何帮助吗???thnx
您需要将Array
构造函数的定义放在标题中,并使用模板的正确语法:
namespace math
{
template<typename T>
Array::Array(unsigned int w, unsigned int h)
{
this->width = w;
this->height = h;
}
}
一个重要的经验法则是,您不能在源文件中的template
类中定义函数。(您可以在某些情况下,但确实将其视为例外而不是规则。)
没有<>
,Array
被假定为在这里并非如此的非模板类,从而导致错误。
您在这里有问题:
namespace imaging
{
class Image: public Array
应该是
namespace imaging
{
template <typename T>
class Image: public Array<T>
相关文章:
- 继承函数的重载解析
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 命名空间中具有.h和.cpp文件的类
- 从父命名空间重载类型
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 通过继承类使用来自不同命名空间的运算符
- 名称空间从C 中的Include File继承
- 有关通过派生类在命名空间内继承的问题
- C 中的继承和名称空间
- 对"vtable for "命名空间继承的未定义引用 对"类型信息"的未定义引用
- 模板类继承命名空间的名称查找
- 继承重载函数:需要命名空间
- 从类继承,在不同的命名空间中定义
- 命名空间继承
- 使用从命名空间继承的类的Eclipse相对缩进
- D&D 游戏的类、命名空间和继承帮助
- c++,命名空间,对继承类和Qt的名称查找
- c++中的命名空间类模板继承
- 命名空间和继承
- c++:从类继承是否会将其带入命名空间