c++ - gcc 的奇怪行为。带有 {} 和 = 的 C++ 对象定义是否相等?
问题描述
我正在测试将处理中完成的模拟转换为 C++ 的可能性。但是奇怪的事情发生了:
class World {
public:
sarray<pAnt> dummy1 { new array<pAnt>(10) };//OK
sarray<pAnt> dummy2 = new array<pAnt>(10); //Compiler error?!?!?!*
smatrix<pAnt> dummyWorld1 { new matrix<pAnt>(100,100) };//OK
smatrix<pAnt> dummyWorld2 = new matrix<pAnt>(100,100);//OK
smatrix<int> ants = new matrix<int>(50,2);// OK
...
};
错误:
/data/wb/SCC/__working_copies/Processing2C/PROJECTS/MROWKI/cppsrc/klasaWorld.pde.cpp:27:
错误:无法将“Processing::sarrayProcessing::ptr<Ant >&”类型的非常量左值引用绑定到“Processing::sarrayProcessing::ptr<Ant >”类型的右值</p>
sarray<pAnt> dummy2 = new array<pAnt>(10) ;//Compiler error?!?
“详细”编译器输出:
dir/data/wb/SCC/__working_copies/Processing2C/PROJECTS/MROWKI/cppsrc/project_at_once.cpp.od -o CMakeFiles/MROWKI_0.1_once.dir/data/wb/SCC/__working_copies/Processing2C/PROJECTS/MROWKI/cppsrc/project_at_once .cpp.o -c /data/wb/SCC/__working_copies/Processing2C/PROJECTS/MROWKI/cppsrc/project_at_once.cpp 使用内置规范。COLLECT_GCC=/usr/bin/g++ OFFLOAD_TARGET_NAMES=nvptx-none OFFLOAD_TARGET_DEFAULT=1 目标:x86_64-linux-gnu 配置:../src/configure -v --with-pkgversion='Ubuntu 7.5.0-3ubuntu1~18.04' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,
GNU C++11 (Ubuntu 7.5.0-3ubuntu1~18.04) 版本 7.5.0 (x86_64-linux-gnu) 由 GNU C 版本 7.5.0、GMP 版本 6.1.2、MPFR 版本 4.0.1、MPC 版本 1.1 编译.0, isl 版本 isl-0.19-GMP ^
这是从我的项目中提取的问题。看起来一行是有罪的,但我不明白为什么:-)
#include <memory>
namespace Processing
{
template<class T>
class ptr
{
//std::shared_ptr<T> _ptr;
public:
ptr();
ptr(ptr<T>&);
//ptr(const ptr<T>&);//This removes the error, but can not be implemented properly!
};
template<class T>
class array
{
//T* content;
public:
~array();
array(size_t N);
};
template<class T>
class sarray
{ // When the following line is commented out the error disapper!
// so what is missing in ptr<T> ?
ptr< array<T> > _arr;//opaque smart ptr to array
//std::shared_ptr< array<T> > _arr;//std smart pointer is OK
public:
~sarray(){}
sarray(){}
sarray(array<T>* tab);
sarray<T>& operator = (array<T>* tab);
};
}
using namespace Processing;
void example()
{
sarray<bool> test1 = new array<bool>(2);// Compiler error?
sarray<bool> test2;
test2 = new array<bool>(2);//OK
sarray<bool> test3 { new array<bool>(2) };//OK
}
解决方案
推荐阅读
- azure - 请验证 Azure 搜索服务是否已启动,重新启动 WebApp 并重试
- javascript - 为什么两个DIV不能在父DIV下拖动?
- powershell - 如何要求 PowerShell 复制标识重复并将其答案以逗号分隔?
- python-3.x - 尝试更改框架时出现 Selenium Python 异常错误
- c# - 使用 HttpClient.GetAsync 块确实注册取消令牌
- swift - 斯威夫特 macOS | 设置(切换)暗模式和亮模式
- json - 在 wso2 esb 中连接两个 JSON 响应
- python - 上传“osmapi.OsmApi.ApiError:请求失败:404 - 未找到 - b”时出错
- abap - OPEN-SQL 查询在abap 4 EXEC SQL 中不起作用
- machine-learning - 训练损失和验证损失总是变为 0.00000e+00