将模板类实例传递给另一个类的构造函数

时间:2014-11-29 12:36:40

标签: c++ templates visual-c++

我的代码:

BlockyWorld.hpp

#ifndef BLOCKYWORLD_H
#define BLOCKYWORLD_H

#include <CImg.h>

namespace logic {
  class BlockyWorld {
  public:
    BlockyWorld( const CImg<float>* heightmap );
  };
}

#endif // BLOCKYWORLD_H

BlockyWorld.cpp

#include "BlockyWorld.hpp"

namespace logic {
  BlockyWorld::BlockyWorld( const CImg<float>* heightmap ) {}
}

的main.cpp

#include <CImg.h>
#include "logic/BlockyWorld.hpp"
//...
CImg<float> heigthMap;
logic::BlockyWorld world( &heigthMap );
//...

编译时我遇到很多错误:

main.cpp中:

include\logic\blockyworld.hpp(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
include\logic\blockyworld.hpp(9): error C2143: syntax error : missing ',' before '<'
main.cpp(85): error C2664: 'logic::BlockyWorld::BlockyWorld(const logic::BlockyWorld &)' : cannot convert argument 1 from 'cimg_library::CImg<float>' to 'const int'
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

BlockyWorld.hpp&amp; CPP

include\logic\blockyworld.hpp(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
include\logic\blockyworld.hpp(9): error C2143: syntax error : missing ',' before '<'
include\logic\blockyworld.cpp(4): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
include\logic\blockyworld.cpp(4): error C2143: syntax error : missing ',' before '<'

我不认为这是一个循环包含错误,有时会导致这些错误=)。 我必须定义构造函数错误或者我定义实现错误?现在正在寻找一个小时的答案,所以我现在真的会用一个解释。

只是为了澄清 - 我不是初学的c / c ++程序员,但这些模板令人困惑:(

祝你有个愉快的一天,感谢你的回答。

1 个答案:

答案 0 :(得分:1)

CImg似乎是cimg_library命名空间的一部分。

using namespace cimg_library添加到BlockyWorld.hpp文件的顶部,或更改函数签名以使用命名空间,如下所示:

BlockyWorld( const cimg_library::CImg<float>* heightmap );

与πάνταῥεῖ一起建议匹配指针和引用类型。