本文介绍了如何修复此错误C2833?前向参考......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 // 运算符140713.cpp:定义控制台应用程序的入口点。 // #include stdafx。 h #include < iostream > 使用 命名空间标准; class 减 { private : int numberminus; public :减号() { numberminus = - 1 ; } void display() { cout<< 减去<< numberminus<< ENDL; } operator Plus() // 错误加号未知 { return Plus(); } }; class 加 { private : int numberplus; public : Plus() { numberplus = 1 ; } void display() { cout<< Plus<< numberplus<< ENDL; } 运算符减号() { return 减(); } }; int _tmain( int argc,_TCHAR * argv []) {减去m,m1; 加p,p1; cout<< OUTPUT<< ENDL; m.display(); p.display(); m1 = p; p1 = m; // 错误 m1.display(); p1.display(); cin.get(); return 0 ; } 如何解决这个问题? Minus类中的Plus运算符不起作用,因为尚未定义Plus类。 当然这一定很容易做对吗?解决方案 运算符Plus()和运算符Minus()运算符不是算术运算符,而是转换(转换)运算符,因此它们很好(这里enhzflep是错误的)。它们每个都返回一个新构造的请求类型的值。 问题只是你在完全定义之前使用它们。 解决这个问题。 1.在Minus()类之前添加以下行: class Plus; 此声明有一个名为 Plus 的类,将在稍后定义。 2.替换 operator +()在课程减去中,包含以下内容: operator Plus(); 这声明了强制转换操作符而没有定义它。 3.在Plus类之后,添加operator Plus()的定义如下: inline 减号:: 运算符 Plus(){ return Plus(); } 在看到所需的定义之后,它定义了类体外的强制转换运算符。 /> 这里的关键思想是函数(或变量或类)的声明和定义可以分开,允许你/> 以避免需要完全定义这两个函数。这在C ++中很常见,因为源代码是从 自上而下处理的。要使用函数,必须先在源代码中声明它,或者(通常)在头文件中包含的声明中声明。 所有那些#include语句只是替换它们的内容直接在源文件中 - 所以包含的声明是 所有可用于代码中的定义。 希望这会有所帮助。 // operators 140713.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <iostream>using namespace std;class Minus{private: int numberminus;public:Minus(){numberminus = -1;}void display(){cout << "Minus " << numberminus << endl;}operator Plus() // Error Plus not known{return Plus();}};class Plus{private: int numberplus;public:Plus(){numberplus = 1;}void display(){cout << "Plus " << numberplus << endl;}operator Minus(){return Minus();}};int _tmain(int argc, _TCHAR* argv[]){Minus m, m1;Plus p, p1;cout << "OUTPUT" << endl;m.display();p.display();m1 = p;p1 = m; // errorm1.display();p1.display();cin.get();return 0;}How do I fix this? The Plus operator in the Minus class doesn't work because the Plus class hasn't been defined yet.Surely this must be easy to do right? 解决方案 The operator Plus() and operator Minus() operators are not arithmetic operators but cast (conversion) operators, so they are fine (enhzflep is wrong here). They each return a newly constructed value of the requested type. The issue is simply that you're using each of them before they're fully defined.To solve this.1. Add the following line before the Minus() class:class Plus;This declares that there is a class called Plus that will be defined later.2. Replace the definition of operator Plus() in class Minus with the following:operator Plus();This declares the cast operator without defining it.3. After the Plus class, add the definition of operator Plus() as follows:inline Minus::operator Plus() { return Plus();}This defines the cast operator outside the body of the class, after the needed definitions have been seen.The key idea here is that a declaration and definition of a function (or variable, or class) can be separated, allowing youto avoid the need for the two functions to be fully defined. This is common in C++, as source code is processed from the top down. To use a function, it must have been declared earlier in the source, or (commonly) in a declaration included from a header.All those #include statements just substitute their contents directly in the source file - so the declarations contained areall available to the definitions in your code.Hope this helps. 这篇关于如何修复此错误C2833?前向参考......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-25 07:11