C++表达式在括号前必须具有(指向)函数类型。

huangapple go评论58阅读模式
英文:

C++ expression preceding parentheses of apparent call must have (pointer-to-) function type

问题

I overloaded parenthesis and I called but I got error.
Code is as written hereunder.

//Matrix43.h
class Matrix43 {
public:
    Matrix43();
    void operator()(
        double, double, double,
        double, double, double,
        double, double, double, 
        double, double, double);
private:
    double m00, m01, m02;
    double m10, m11, m12;
    double m20, m21, m22;
    double m30, m31, m32;
};
//Matrix43.cpp
#include "Matrix43.h"

Matrix43::Matrix43() :
    m00(0.0), m01(0.0), m02(0.0),
    m10(0.0), m11(0.0), m12(0.0),
    m20(0.0), m21(0.0), m22(0.0),
    m30(0.0), m31(0.0), m32(0.0) {
}
void Matrix43::operator()(
    double e00, double e01, double e02,
    double e10, double e11, double e12, 
    double e20, double e21, double e22, 
    double e30, double e31, double e32) {
    m00 = e00; m01 = e01; m02 = e02;
    m10 = e10; m11 = e11; m12 = e12;
    m20 = e20; m21 = e21; m22 = e22;
    m30 = e30; m31 = e31; m32 = e32;
}

I call operator like this and got an error.

//main.cpp
#include "Matrix43.h"

Matrix43* a;

int main() {
    a = new Matrix43;
    // C++ expression preceding parentheses of apparent call must have (pointer-to-) function type
    *a(
        -150.0, -100.0, -300.0,
        150.0, -100.0, -300.0,
        -150.0, -100.0, 300.0,
        150.0, -100.0, 300.0
    );
    delete a;
    return 0;
}

I found using a local variable which has a reference type can solve this error.

#main.cpp
#include "Math/Matrix43.h"

Matrix43* a;

int main() {
    a = new Matrix43;
    Matrix43& o = *a;
    o(
        -150.0, -100.0, -300.0,
        150.0, -100.0, -300.0,
        -150.0, -100.0, 300.0,
        150.0, -100.0, 300.0
    );
    delete a;
    return 0;
}

But I would like to call the operator through using *a so as to know exactly a is calling that.
Is there any solution?

These codes are parts of a project, some points may look strange.
If you have noticed, please tell me and I will add code properly.

Kind regards

英文:

I overloaded parenthesis and I called but I got error.
Code is as written hereunder.

//Matrix43.h
class Matrix43 {
public:
	Matrix43();
	void operator()(
		double, double, double,
		double, double, double,
		double, double, double, 
		double, double, double);
private:
	double m00, m01, m02;
	double m10, m11, m12;
	double m20, m21, m22;
	double m30, m31, m32;
};
//Matrix43.cpp
#include "Matrix43.h"

Matrix43::Matrix43() :
	m00(0.0), m01(0.0), m02(0.0),
	m10(0.0), m11(0.0), m12(0.0),
	m20(0.0), m21(0.0), m22(0.0),
	m30(0.0), m31(0.0), m32(0.0) {
}
void Matrix43::operator()(
	double e00, double e01, double e02,
	double e10, double e11, double e12, 
	double e20, double e21, double e22, 
	double e30, double e31, double e32) {
	m00 = e00; m01 = e01; m02 = e02;
	m10 = e10; m11 = e11; m12 = e12;
	m20 = e20; m21 = e21; m22 = e22;
	m30 = e30; m31 = e31; m32 = e32;
}

I call operator like this and got error.

//main.cpp
#include "Matrix43.h"

Matrix43* a;

	int main() {
		a = new Matrix43;
//C++ expression preceding parentheses of apparent call must have (pointer-to-) function type
		*a(
			-150.0, -100.0, -300.0,
			150.0, -100.0, -300.0,
			-150.0, -100.0, 300.0,
			150.0, -100.0, 300.0
		);
		delete a;
                return 0;
	}

I found using local variable which has reference type can solve this error.

#main.cpp
#include "Math/Matrix43.h"

Matrix43* a;

	int main() {
		a = new Matrix43;
		Matrix43& o = *a;
		o(
			-150.0, -100.0, -300.0,
			150.0, -100.0, -300.0,
			-150.0, -100.0, 300.0,
			150.0, -100.0, 300.0
		);
		delete a;
                return 0
	}

But I would like to call operator through using *a so as to know exactly a is calling that.
Is there any solutions?

those codes are parts of a project, some points may looks strange.
If you have noticed, please tell me and I will add code propery.
(In the project that main function loops and I need to initialize value without constructor.)

Kind regards

答案1

得分: 1

当你写:

*a(1,2,3,4,5,6,7,8,9);

它被解析为:

*(a(1,2,3,4,5,6,7,8,9));

这不是你想要的结果。所以你需要更明确地使用括号,并且这样写:

(*a)(1,2,3,4,5,6,7,8,9);
英文:

When you write:

*a(1,2,3,4,5,6,7,8,9);

It is parsed as:

*(a(1,2,3,4,5,6,7,8,9));

Which is not what you wanted. So you're going to need to be more explicit with parentheses and do:

(*a)(1,2,3,4,5,6,7,8,9);

huangapple
  • 本文由 发表于 2023年6月12日 07:40:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452925.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定