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

Passing template class instance to a constructor of another class

本文关键字:另一个 构造函数 实例      更新时间:2023-10-16

我的代码:

BlockyWorld.hpp

#ifndef BLOCKYWORLD_H
#define BLOCKYWORLD_H
#include <CImg.h>
namespace logic {
  class BlockyWorld {
  public:
    BlockyWorld( const CImg<float>* heightmap );
  };
}
#endif // BLOCKYWORLD_H

块状世界.cpp

#include "BlockyWorld.hpp"
namespace logic {
  BlockyWorld::BlockyWorld( const CImg<float>* heightmap ) {}
}

主.cpp

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

编译时出现很多错误:

主.cpp:

includelogicblockyworld.hpp(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
includelogicblockyworld.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 & cpp

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

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

澄清一下 - 我不是初学者 c/c++ 程序员,但这些模板令人困惑:(

祝你有美好的一天,感谢你的回答。

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

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

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

以及 πάντα ῥεῖ 关于匹配指针和引用类型的建议。