1

我在不同的文件中描述了类:

3DObjectDescription.h

#pragma once
#include <SFML/Graphics.hpp>
#include <NumCpp.hpp>
#include "MoveMatrix.h"

class Object3D
{
public:
    nc::NdArray<float> vertexes;

    nc::NdArray<float> faces;

    Object3D();

    void translation(float tX, float tY, float tZ);
    void rotate_x(float angle);
    void rotate_y(float angle);
    void rotate_z(float angle);
    void scale_(float scale_c);

};

3DObjectDescription.cpp

#include "3DObjectDescription.h"

Object3D::Object3D()
{
    vertexes = {{0,0,0,1}, {0,1,0,1}, {1,1,0,1}, {1,0,0,1},
                {0,0,1,1}, {0,1,1,1}, {1,1,1,1}, {1,0,1,1}};

    faces = {{0,1,2,3}, {4,5,6,7}, {0,4,5,1}, {2,3,7,6}, {1,2,6,5}, {0,3,7,4}};
}

Object3D::translation(float tX, float tY, float tZ)
{
    vertexes = nc::dot(vertexes, translate(tX, tY, tZ));
}

Object3D::rotate_x(float angle_)
{
    vertexes = nc::dot(vertexes, rotation_X(angle_));
}

Object3D::rotation_y(float angle_)
{
    vertexes = nc::dot(vertexes, rotation_Y(angle_));
}

Object3D::rotation_z(float angle)
{
    vertexes = nc::dot(vertexes, rotation_Z(angle_));
}

Object3D::scale_(float scale_c)
{
    vertexes = nc::dot(vertexes, scale(scale_c));
}

当我编译它时,我得到错误:

错误:没有声明匹配 'int Object3D::translation(float, float, float)'
错误:没有声明匹配 'int Object3D::rotate_x(float)'
错误:没有声明匹配 'int Object3D::rotation_y(float)'
错误: 没有声明匹配 'int Object3D::rotation_z(float)'
错误: 没有声明匹配 'int Object3D::scale_(float)'

当我没有声明它时,我不知道整数类型是从哪里来的!我从来没有遇到过这样的错误!

4

2 回答 2

1

声明返回类型是void.
void translation(float tX, float tY, float tZ);
 ^^^^

但是您的定义没有类型。
void Object3D::translation(float tX, float tY, float tZ)
 ^^^^ 不见了


当缺少显式返回类型时,某些编译器会假定为“int”,从而产生该错误。

例如 VS2019 错误:
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

于 2021-03-03T16:27:31.570 回答
1

中的所有定义3DObjectDescription.cpp在其签名中都缺少返回类型。

只需更新他们的签名以包含void返回类型。的定义Object3D::translation应该阅读

void Object3D::translation(float tX, float tY, float tZ)
{
    vertexes = nc::dot(vertexes, translate(tX, tY, tZ));
}

其余的也是如此。


您的编译器抱怨签名不匹配而不是指示语法错误的原因是因为您的编译器隐式假设返回类型为intwhen none 指定。这是 C 的保留。但是,这种特性已在 C++ 中删除,因此您的编译器通过尝试编译它会表现出非标准行为。

于 2021-03-03T16:24:36.210 回答