VC++2015报错:在没有适当operator()的情况下调用类类型
VC2015环境,问题在代码里面,请看下面代码
//声明点结构struct Point3D
{
GLfloat x, y, z;
Point3D(GLfloat _x, GLfloat _y, GLfloat _z) : x(_x), y(_y), z(_z) {}
};
//在类里用,未初始化
class A
{
public:
A();
~A();
//在类里声明并初始化数组,允许,不报错
GLfloat BG_RGBA[4] = {0.22f,0.24f,0.27f,1.0f};
//如果声明并初始化自定义类型,报错,怎么做这里才能向上面一样不报错?
Point3D LastPoint(0,0,0);
void F(void);
}
A::F()
{
//报错:在没有适当operator()的情况下调用类类型
LastPoint(0,0,0)
//感觉太烦锁,怎么样才能像上面一样写法?
LastPoint.x = 0;
LastPoint.y = 0;
LastPoint.z = 0;
}
回答:
- 上面的代码反应了似乎你的 C++ 基础不是很好,出现多处明显语法错误
如果可以,建议直接学习 C++11 及后续版本
#include <iostream>
using namespace std;
class Point3D
{
public:
float x, y, z;
Point3D(float _x, float _y, float _z) : x(_x), y(_y), z(_z) {}
void operator ()(int a, int b, int c)
{
cout << a << " " << b << " " << c << endl;
}
};
class A
{
public:
A() = default;
~A() = default;
float BG_RGBA[4] = {0.22f,0.24f,0.27f,1.0f};
Point3D LastPoint{0, 0, 0}; // 注意此处的初始化方式 C++11/14 新版本支持
Point3D LastPoint1 = Point3D(0, 1, 1); // 经典 C++ 支持
void F();
};
void A::F()
{
LastPoint(3,3,3);
LastPoint.x = 0;
LastPoint.y = 0;
LastPoint.z = 0;
}
int main()
{
A a;
a.F();
return 0;
}
输出:
3 3
以上是 VC++2015报错:在没有适当operator()的情况下调用类类型 的全部内容, 来源链接: www.h5w3.com/193374.html