我是新來的在C++編程。我對Java有很好的背景,但是C++在很多方面都不同,我對於.h和.cpp文件中的一個有疑問。C++類的方法調用.cpp沒有::
我有x和y位置的點對象中的以下文件:
Point.h
#ifndef POINT_H_
#define POINT_H_
class Point{
Point();
Point(int newX, int newY);
public:
int getX();
int getY();
void setX(int newX);
void setY(int newY);
void moveBy(int moveX, int moveY);
Point reverse();
private:
int x;
int y;
};
#endif
Point.cpp
#include "Point.h"
using namespace Point;
Point::Point(int newX, int newY){
x = newX;
y = newY;
}
int Point::getX(){
return x;
}
int Point::getY(){
return y;
}
void Point::setX(int newX){
x = newX;
}
void Point::setY(int newY){
y = newY;
}
void Point::moveBy(int moveX, int moveY){
x += moveX;
y += moveY;
}
Point Point::reverse(){
return Point(y,x);
}
我在想,如果有一個通過使用命名空間來避免Point::Point
部分與std :: cout類似的方式。
謝謝
沒有,因爲'Point'不是一個命名空間 –
不要把函數定義在.cpp文件,只是把它們放在類定義。 – kfsone