关于类的蔡鸟有关问题
关于类的蔡鸟问题
有如下代码:
//Point.h
ifndef POINT_H
#define POINT_H
typedef long double REAL;
class Point
{
public:
Point(const REAL&, const REAL&);
const REAL operator[](int) const;
private:
REAL x, y;
};
#endif
//Point.cpp
#include <iostream>
#include "Point.h "
Point::Point(const REAL& xx=0.0, const REAL& yy=0.0)
{
x = xx;
y = yy;
};
const REAL Point::operator[](int i) const
{
if (i==1) return x;
else if (i==2) return y;
else {
cout < < "No such a element! " < < endl;
break;
};
};
//test2.cpp
#include <iostream>
#include "Point.h "
using namespace std;
int main()
{
Point p(1.0, 2.0);
REAL a = p[1];
cout < <a < <endl;
return 0;
}
这个程序编译通过,但是不能连接:
test2.obj : error LNK2001: unresolved external symbol "public: long double const __thiscall Point::operator[](int)const " (??APoint@@QBE?BOH@Z)
test2.obj : error LNK2001: unresolved external symbol "public: __thiscall Point::Point(long double const &,long double const &) " (??0Point@@QAE@ABO0@Z)
Debug/test2.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.
没看懂是什么意思,请大家指点迷津。
------解决方案--------------------
检查函数接口或者声明之类的
------解决方案--------------------
1、ifndef改成#infndef
2、if语句多了break;break是循环语句和switch语句中才用到的。
------解决方案--------------------
笔误多写了个n应该是#ifndef
------解决方案--------------------
//Point.cpp 这里面小改一下
#include <iostream>
#include "Point.h "
using namespace std;
Point::Point(const REAL& xx=0.0, const REAL& yy=0.0)
{
x = xx;
y = yy;
};
const REAL Point::operator[](int i) const
{
if (i==1) return x;
else if (i==2) return y;
else
cout < < "No such a element! " < < endl;
return -1;
};
------解决方案--------------------
break;可以去掉。
------解决方案--------------------
typedef long double REAL;
private:
REAL x, y;
....?
------解决方案--------------------
#ifndef POINT_H_
#define POINT_H_
typedef long double REAL;
class Point
{
public:
Point(const REAL & xx, const REAL & yy);
const REAL operator[](int)const;
private:
REAL x, y;
};
#endif
有如下代码:
//Point.h
ifndef POINT_H
#define POINT_H
typedef long double REAL;
class Point
{
public:
Point(const REAL&, const REAL&);
const REAL operator[](int) const;
private:
REAL x, y;
};
#endif
//Point.cpp
#include <iostream>
#include "Point.h "
Point::Point(const REAL& xx=0.0, const REAL& yy=0.0)
{
x = xx;
y = yy;
};
const REAL Point::operator[](int i) const
{
if (i==1) return x;
else if (i==2) return y;
else {
cout < < "No such a element! " < < endl;
break;
};
};
//test2.cpp
#include <iostream>
#include "Point.h "
using namespace std;
int main()
{
Point p(1.0, 2.0);
REAL a = p[1];
cout < <a < <endl;
return 0;
}
这个程序编译通过,但是不能连接:
test2.obj : error LNK2001: unresolved external symbol "public: long double const __thiscall Point::operator[](int)const " (??APoint@@QBE?BOH@Z)
test2.obj : error LNK2001: unresolved external symbol "public: __thiscall Point::Point(long double const &,long double const &) " (??0Point@@QAE@ABO0@Z)
Debug/test2.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.
没看懂是什么意思,请大家指点迷津。
------解决方案--------------------
检查函数接口或者声明之类的
------解决方案--------------------
1、ifndef改成#infndef
2、if语句多了break;break是循环语句和switch语句中才用到的。
------解决方案--------------------
笔误多写了个n应该是#ifndef
------解决方案--------------------
//Point.cpp 这里面小改一下
#include <iostream>
#include "Point.h "
using namespace std;
Point::Point(const REAL& xx=0.0, const REAL& yy=0.0)
{
x = xx;
y = yy;
};
const REAL Point::operator[](int i) const
{
if (i==1) return x;
else if (i==2) return y;
else
cout < < "No such a element! " < < endl;
return -1;
};
------解决方案--------------------
break;可以去掉。
------解决方案--------------------
typedef long double REAL;
private:
REAL x, y;
....?
------解决方案--------------------
#ifndef POINT_H_
#define POINT_H_
typedef long double REAL;
class Point
{
public:
Point(const REAL & xx, const REAL & yy);
const REAL operator[](int)const;
private:
REAL x, y;
};
#endif