Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 这两个错误的原因是什么:赋值运算符未生成和未引用形式参数?_C++_Error Handling - Fatal编程技术网

C++ 这两个错误的原因是什么:赋值运算符未生成和未引用形式参数?

C++ 这两个错误的原因是什么:赋值运算符未生成和未引用形式参数?,c++,error-handling,C++,Error Handling,我有一段代码和一些错误和警告,我无法理解它们为什么会发生。你能给我一些建议吗 第一: #pragma once #include "Vector3.h" #include <vector> #include <GL/glew.h> class BoundingSphere { public: float radius; Vector3 center; BoundingSphere(float radius, Vector3

我有一段代码和一些错误和警告,我无法理解它们为什么会发生。你能给我一些建议吗

第一:

#pragma once

#include "Vector3.h"
#include <vector>

#include <GL/glew.h>

class BoundingSphere
{
public:
    float radius;
    Vector3 center;
    BoundingSphere(float radius, Vector3 center) : radius(radius),center(center) {};
    BoundingSphere() {};
};

class TriangleFace;

class MeshVertex
{

private:
    Vector3 position; 
    std::vector<TriangleFace *> faces;
    Vector3 normal;
    bool normalUpdateNeeded;

public:

    unsigned int index;

    MeshVertex(void);
    MeshVertex(Vector3 position);
    ~MeshVertex(void);

    Vector3 &getPos() {return position;};
    void addFace(TriangleFace *face);
    const std::vector<TriangleFace*>& getFaces() {return faces;  };
    Vector3 getNormal();

    void setPos(Vector3 & pos) {position = pos; }
    bool isSurfaceParticle() {return faces.size()>0;}
    void updateNormal();
};

class TriangleFace
{
private:
    Vector3 normal;
    bool normalUpdateNeeded;

public:
    MeshVertex* particles[3];
    TriangleFace(void);
    TriangleFace(MeshVertex *p1, MeshVertex *p2, MeshVertex *p3);
    MeshVertex& operator[](int i) {  return *(particles[i]); }
    Vector3 getNormal();
    ~TriangleFace(void);
    void updateNormal();
};

class TriangleMesh
{
protected:
    std::vector<MeshVertex> particles;
    std::vector<TriangleFace> faces;

public:
    TriangleMesh(string filename);
    ~TriangleMesh(void);

    void reserveNumberOfFaces(unsigned int n) { faces.reserve(n); };
    void addFace(TriangleFace &f) {faces.push_back(f);};
    void addFace(MeshVertex *p1, MeshVertex *p2, MeshVertex *p3) {faces.push_back(TriangleFace(p1,p2,p3));};

    std::vector<TriangleFace>& getFaces() {return faces;};
    std::vector<MeshVertex>& getParticles() { return particles; };

    void updateNormals();

    BoundingSphere getBoundingSphere();
};


class RenderTriangleMesh
{
private: 
    TriangleMesh &m;

    GLuint vboid[2];

    GLfloat *vertices;
    GLfloat *normals;

public:
    RenderTriangleMesh(TriangleMesh &m);
    void draw();

private:
    void generateVBOs();
    void updateVBOs();

};
错误:

错误C2220:警告被视为错误-未生成“对象”文件

警告C4100:“gm”:未引用的形式参数


第一种说法是编译器无法生成赋值运算符。发生这种情况的原因是引用成员,因为引用无法重置:

struct foo
{
    int& i;
};

int x, y;
foo f = { x };
foo g = { y };

f = g; // ??? 
您可以通过自己显式禁用警告使其静音:

struct foo
{
    int& i;

private:
    foo& operator=(const foo&); // declared but never defined
};
这是一个使赋值运算符不可用的老把戏,但会阻止编译器生成它

在C++11中,可以执行以下操作:

struct foo
{
    int& i;

private:
    foo& operator=(const foo&) = delete;
};
这是更明确和更少的黑客


另一个原因是您从未使用过参数,这通常是程序逻辑中的错误。如果不使用它,您可能应该删除它的名称:

virtual short SimpleAddOK(const GeneralMatrix* /* gm */) { return 0; } 
就我个人而言,我完全删除了它。其他人这样做:

virtual short SimpleAddOK(const GeneralMatrix* gm)
{
    (void)gm; // this "uses" gm

    return 0;
} 
它有时被包装在一个名为
USE
的宏中:

#define USE(x) (void)x

virtual short SimpleAddOK(const GeneralMatrix* gm)
{
    USE(gm);

    return 0;
} 
我认为这些都不如简单地删除这个名字,因为如果警告实际上是正确的(也就是说,你的程序中有一个缺陷而没有看到它),你就屏蔽了警告,破坏了目的。
警告应该始终是固定的,而不是沉默的。

第一个是说编译器无法生成赋值运算符。发生这种情况的原因是引用成员,因为引用无法重置:

struct foo
{
    int& i;
};

int x, y;
foo f = { x };
foo g = { y };

f = g; // ??? 
您可以通过自己显式禁用警告使其静音:

struct foo
{
    int& i;

private:
    foo& operator=(const foo&); // declared but never defined
};
这是一个使赋值运算符不可用的老把戏,但会阻止编译器生成它

在C++11中,可以执行以下操作:

struct foo
{
    int& i;

private:
    foo& operator=(const foo&) = delete;
};
这是更明确和更少的黑客


另一个原因是您从未使用过参数,这通常是程序逻辑中的错误。如果不使用它,您可能应该删除它的名称:

virtual short SimpleAddOK(const GeneralMatrix* /* gm */) { return 0; } 
就我个人而言,我完全删除了它。其他人这样做:

virtual short SimpleAddOK(const GeneralMatrix* gm)
{
    (void)gm; // this "uses" gm

    return 0;
} 
它有时被包装在一个名为
USE
的宏中:

#define USE(x) (void)x

virtual short SimpleAddOK(const GeneralMatrix* gm)
{
    USE(gm);

    return 0;
} 
我认为这些都不如简单地删除这个名字,因为如果警告实际上是正确的(也就是说,你的程序中有一个缺陷而没有看到它),你就屏蔽了警告,破坏了目的。
警告应该始终是固定的,而不是沉默的。

您发布的代码似乎与错误消息不匹配(您发布的代码中甚至没有25行)。您是否可以显示您编写的代码,而不是标准库中的代码?您的标题“error C2143;error C2059;error C2143;error C2447”仅对熟悉MS Visual Studio并碰巧记住了其错误号的人有意义。其他编译器不使用这些数字。消息的文本,例如“语法错误:缺少”;“字符串”之前的“更具意义——尽管您可能不需要在标题中说“语法错误”。我编辑了我的问题,感谢您发布的代码似乎与错误消息不匹配(您发布的代码中甚至没有25行)。您是否可以显示您编写的代码,而不是标准库中的代码?您的标题“error C2143;error C2059;error C2143;error C2447”仅对熟悉MS Visual Studio并碰巧记住了其错误号的人有意义。其他编译器不使用这些数字。消息的文本,例如“语法错误:缺少”;“字符串”之前的“更具意义——尽管您可能不需要在标题中说“语法错误”。我已经编辑了我的问题,谢谢