求大神幫我看看怎么修改
下面是部分代碼
Line.cpp
#include<iostream>
#include"Line.h"
using namespace std;
? Line::Line(int x1, int y1, int x2, int y2) :m_coorA(x1,y1), m_coorB(x2, y2)
{
cout << "Line()" << endl;
}
Line::~Line()
{
cout << "~Line()" << endl;
}
void Line::setA(int x, int y)
{
m_coorA.setX(x);
m_coorA.setY(y);
}
void Line::setB(int x, int y)
{
m_coorB.setX(x);
m_coorB.setY(y);
}
void Line::printInfo()
{
cout << "printInfo()" << endl;
cout << "("m_coorA.getX() << "," << m_coorA.getY() << ")" << endl;
cout << "("m_coorB.getX() << "," << m_coorB.getY() << ")" << endl;
}
void Line::printInfo() const
{
cout << "printInfo()const" << endl;
cout << "("m_coorA.getX() << "," << m_coorA.getY() << ")" << endl;
cout << "("m_coorB.getX() << "," << m_coorB.getY() << ")" << endl;
}
Line.h
#include"Coordinate.h"
class Line
{
public:
Line(int x1, int y1, int x2, int y2);
~Line();
void setA(int x, int y);
void setB(int x, int y);
void printInfo();
void printInfo() const;
private:
const Coordinate m_coorA;
Coordinate m_coorB;
};
前兩個黑體部分錯誤提示:不兼容的類型限定符;
后四個黑體部分錯誤提示:未找到用戶定義的文本運算符。
2018-08-23
常對象只能調(diào)用常成員函數(shù)。 m_coorA為常對象,而setA 為普通成員函數(shù),m_coorA.setA(T* this,int x,int y)參數(shù) T* this指的當(dāng)前對象有可讀可寫權(quán)限,而m_coorA 只有可讀權(quán)限??隙ㄊ遣豢梢缘?。
2018-08-23
你只要記住一句話 ,常對象只能調(diào)用常對象成員函數(shù)。別的編譯器會報錯。修改的話m_coorA這個對象不要去調(diào)用setA或者把setA 這個成員函數(shù)修改為常成員函數(shù) 使用初始化列表進行賦值。(PS 或者不可行)