读书人

关于类的蔡鸟有关问题

发布时间: 2012-03-14 12:01:12 作者: rapoo

关于类的蔡鸟问题
有如下代码:
//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



#include <iostream>
#include "Point.h "
using namespace std;
REAL x,y;

Point::Point(const REAL& xx, const REAL& yy)
{

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;
}

};


#include <iostream>
#include "Point.h "

int main()
{
using namespace std;
Point p(1.0, 2.0);
REAL a = p[1];
cout < <a < <endl;
return 0;
}

[解决办法]
把typedef long double REAL; 与在类声明里试试呢?

[解决办法]
怎么可能呢?
你建立一个工程,把这3个文件包含进去,在point.cpp中加一句 using namespace std;再把那个break去掉,编译,运行,结果输出为 1
[解决办法]
蹭分

读书人网 >C++

热点推荐