将模板类实例传递给另一个类的构造函数
Passing template class instance to a constructor of another class
我的代码:
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 );
以及 πάντα ῥεῖ 关于匹配指针和引用类型的建议。
相关文章:
- 为什么从另一个构造函数内部调用C++构造函数不修改类变量?
- 如何处理没有默认构造函数但在另一个构造函数中构造的对象?
- C++ 如何根据作为输入传递的参数调用一个构造函数或另一个构造函数?
- 让构造函数在其初始化列表中调用同一类的另一个构造函数是否有效
- 我如何在类上需要另一个构造函数的对象中声明一个对象
- 使用委托调用构造函数和在另一个构造函数体中调用的区别
- C :另一个构造函数的构造函数的隐式调用
- 从同一类的另一个构造函数调用构造函数
- 将类的实例传递给另一个构造函数,该构造函数将其对象添加到所传递实例所拥有的列表中
- C++构造函数调用另一个构造函数
- 构造具有const成员的对象时调用另一个构造函数
- 为另一个构造函数中的对象数组调用构造函数
- 在另一个构造函数中调用构造函数(没有要调用的匹配函数..) c++
- 不能从同一类的另一个构造函数调用具有所有默认参数的显式构造函数
- 为什么在添加另一个构造函数时会出现此错误
- 调用同一类中的另一个构造函数
- 在另一个构造函数中实例化的对象的构造函数内调用函数
- 是否有可能在c++中直接在另一个构造函数的参数中构造一个对象?
- 用数组初始化从另一个构造函数调用c++构造函数
- c++在同一类的另一个构造函数中调用构造函数