自制类的数组[2]导致问题 [英] Selfcreated class an array[2] is causing problems

查看:101
本文介绍了自制类的数组[2]导致问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!任何时候我想编译我得到错误信息(C ++禁止指针和整数之间的比较; RecordLu使用但未定义)。为什么?



< pre>  #include   <   iostream  >  
#include < array >

使用 命名空间标准
class Punkt {
public int XYCoord [ 2 ] = {};
void setupCoord( int x, int y){
XYCoord [ 0 ] = x;
XYCoord [ 1 ] = y;
}
};
class Rechteck {
public :Punkt ReCoordLu,ReCoordRo;

double flaeche( double x, double y){
double xy = x * y;
return xy;
}
bool 包含(Punkt& p){
Punkt * ConTemp1 =& p;
Punkt ConTemp;
ConTemp = * ConTemp1;

if (ConTemp.XYCoord [ 0 ]> =&& ; ReCoordLu.XYCoord [ 0 ]&& ConTemp.XYCoord [ 1 ]< = ReCoordRo。 XYCoord [ 1 ]){
return true ;}
else {
return false ;}
};
bool 包含(Rechteck&){
if 1 ){
return true ;}
else
return false ;
}
};
int main()
{ / * Rechteck sharedRectangle(Rechteck a,Rechteck b){
Rechteck c;
返回Rechteck c;
} * /


Punkt P1,P2;
P1.setupCoord( 1 1 );
P2.setupCoord( 5 5 );
Rechteck Rechteck1;
Rechteck1.ReCoordLu.setupCoord( 3 3 );
Rechteck1.ReCoordRo.setupCoord( 9 9 );

cout<< hello<< P2.XYCoord [ 0 ]<< Rechteck1.ReCoordLu.XYCoord [ 0 ]<< Rechteck1.flaeche ( 5 5 );

return 0 ;};





我尝试了什么:



Punkt ReCordLu公共功能包含到认识到它。修改过的代码但无法找到问题。

解决方案

错误消息包含行号。那么看看那一行:

 if(ConTemp.XYCoord [0]> =&& ReCoordLu.XYCoord [0]&&& ConTemp.XYCoord [1]< = ReCoordRo.XYCoord [1])



你现在看到了吗?它必须是

 if(ConTemp.XYCoord [0]> = ReCoordLu.XYCoord [0]&& ConTemp.XYCoord [1]< = ReCoordRo.XYCoord [1])



这两个&& 被视为指针运算符的地址。


好的,现在我正在工作,但我不明白为什么P4(3,7)给出返回值true(1),因为这里7(ConTemp.XYCoord [1])大于5(ReCoordRo.XYCoord [1])?



< pre>  #include   <   iostream  >  
#include < array >

使用 命名空间性病;
class Punkt {
public int XYCoord [ 2 ] = {};
void setupCoord( int x, int y){
XYCoord [ 0 ] = x;
XYCoord [ 1 ] = y;
}
};
class Rechteck {
public :Punkt ReCoordLu,ReCoordRo;

double flaeche( double x, double y){
double xy = x * y;
return xy;
}
bool 包含(Punkt& p){
Punkt * ConTemp1 =& p;
Punkt ConTemp;
ConTemp = * ConTemp1;

if (ConTemp.XYCoord [ 0 ]> = ReCoordLu.XYCoord [ 0 ]&& ConTemp.XYCoord [ 1 ]< = ReCoordRo.XYCoord [ 1 ]){
return true ;}
else {
return false ;}
};
bool 包含(Rechteck&){
if 1 ){
return true ;}
else
return false ;
}
};
int main()
{ / * Rechteck sharedRectangle(Rechteck a,Rechteck b){
Rechteck c;
返回Rechteck c;
} * /


Punkt P1,P2,P3,P4;
P1.setupCoord( 1 1 );
P2.setupCoord( 5 5 );
P3.setupCoord( 3 3 );
P4.setupCoord( 3 7 );
Rechteck Rechteck1;
Rechteck1.ReCoordLu.setupCoord( 3 3 );
Rechteck1.ReCoordRo.setupCoord( 9 9 );


cout<< hello<< ; P2.XYCoord [ 0 ]<< Rechteck1.ReCoordLu.XYCoord [ 0 ]
<<< Rechteck1.flaeche( 5 5 )<< Rechteck1.contains(P4 );

return 0 ;};


Hi ! Anytime i want to compile i get error message (C++ forbides comparisons between pointers and integers; RecordLu used but not defined ).Why ?

<pre>#include <iostream>
#include <array>

 using namespace std;
    class Punkt {
        public: int XYCoord [2]={};
        void setupCoord (int x, int y){
            XYCoord[0]=x;
            XYCoord[1]=y;
        }
    };
    class Rechteck {
        public: Punkt ReCoordLu,ReCoordRo;

        double flaeche(double x, double y){
            double xy=x*y;
            return xy;
            }
        bool contains ( Punkt &p){
            Punkt *ConTemp1=&p;
            Punkt ConTemp ;
            ConTemp = *ConTemp1;

            if (ConTemp.XYCoord[0]>=&&ReCoordLu.XYCoord[0]&&ConTemp.XYCoord[1]<=ReCoordRo.XYCoord[1]){
                return true;}
            else{
                return false;}
                };
        bool contains (Rechteck &){
            if (1){
                return true;}
            else
                return false;
            }
        };
int main()
{/* Rechteck sharedRectangle (Rechteck a , Rechteck b){
            Rechteck c;
            return Rechteck c;
            } */

        Punkt P1,P2;
        P1.setupCoord(1,1);
        P2.setupCoord(5,5);
        Rechteck Rechteck1;
        Rechteck1.ReCoordLu.setupCoord(3,3);
        Rechteck1.ReCoordRo.setupCoord(9,9);

        cout<<"hello"<<P2.XYCoord[0]<<Rechteck1.ReCoordLu.XYCoord[0]<<Rechteck1.flaeche(5,5);

        return 0;};



What I have tried:

made Punkt ReCordLu public for function contain to recognize it. Revised code but cant find problem.

解决方案

The error message contains a line number. So have a look at that line:

if (ConTemp.XYCoord[0]>=&&ReCoordLu.XYCoord[0]&&ConTemp.XYCoord[1]<=ReCoordRo.XYCoord[1])


Do you see it now? It must be

if (ConTemp.XYCoord[0]>=ReCoordLu.XYCoord[0]&&ConTemp.XYCoord[1]<=ReCoordRo.XYCoord[1])


The two && are treated as address of operators here which are pointers.


Okay thx now i it working, but i dont understand why P4(3,7)is giving returnvalue true (1) , since here 7(ConTemp.XYCoord[1]) is bigger than 5 (ReCoordRo.XYCoord[1]) ?

<pre>#include <iostream>
#include <array>

 using namespace std;
    class Punkt {
        public: int XYCoord [2]={};
        void setupCoord (int x, int y){
            XYCoord[0]=x;
            XYCoord[1]=y;
        }
    };
    class Rechteck {
        public: Punkt ReCoordLu,ReCoordRo;

        double flaeche(double x, double y){
            double xy=x*y;
            return xy;
            }
        bool contains ( Punkt &p){
            Punkt *ConTemp1=&p;
            Punkt ConTemp ;
            ConTemp = *ConTemp1;

            if (ConTemp.XYCoord[0]>=ReCoordLu.XYCoord[0]&&ConTemp.XYCoord[1]<=ReCoordRo.XYCoord[1]){
                return true;}
            else{
                return false;}
                };
        bool contains (Rechteck &){
            if (1){
                return true;}
            else
                return false;
            }
        };
int main()
{/* Rechteck sharedRectangle (Rechteck a , Rechteck b){
            Rechteck c;
            return Rechteck c;
            } */

        Punkt P1,P2,P3,P4;
        P1.setupCoord(1,1);
        P2.setupCoord(5,5);
        P3.setupCoord(3,3);
        P4.setupCoord(3,7);
        Rechteck Rechteck1;
        Rechteck1.ReCoordLu.setupCoord(3,3);
        Rechteck1.ReCoordRo.setupCoord(9,9);


        cout<<"hello"<<P2.XYCoord[0]<<Rechteck1.ReCoordLu.XYCoord[0]
        <<Rechteck1.flaeche(5,5)<<Rechteck1.contains(P4) ;

        return 0;};


这篇关于自制类的数组[2]导致问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆