关于map在头文件中的问题

Jason_Yao 2008-11-08 01:00:28
有一个线性回归程序
#pragma   warning   (disable:4786)

//#include "RegressionLine.hpp"
#include <map>
#include <string>
#include <iostream>
#include <math.h>


using namespace std;



class RegressionLine
{
public:
typedef map<double,double> Points;
RegressionLine(Points & points);
virtual ~RegressionLine();

const double slope() const;
const double yIntercept() const;
const double regressionCoefficient() const;

private:

double slope_;
double yIntercept_;
double regressionCoefficient_;

};
// 上面是定义一个类,如果把它作为一个头文件,放在.hpp文件中的话,编译就会出现很多错误。而把typedef拿出来后,把这段代码贴到.cpp文件
// 的前面的话,程序就能运行了,请问这是为什么啊?

RegressionLine::RegressionLine(Points & points)
{
int n = points.size();
if (n < 2)
throw (string("Must have at least two points"));


double sumx=0,sumy=0,sumx2=0,sumy2=0,sumxy=0;
double sxx,syy,sxy;

// Conpute some things we need
map<double, double>::const_iterator i;
for (i = points.begin(); i != points.end(); i++)
{
double x = i->first;
double y = i->second;

sumx += x;
sumy += y;
sumx2 += (x * x);
sumy2 += (y * y);
sumxy += (x * y);
}
sxx = sumx2 - (sumx * sumx / n);
syy = sumy2 - (sumy * sumy / n);
sxy = sumxy - (sumx * sumy / n);

// Infinite slope_, non existant yIntercept
if (abs(sxx) == 0)
throw (string("Inifinite Slope"));

// Calculate the slope_ and yIntercept
slope_ = sxy / sxx;
yIntercept_ = sumy / n - slope_ * sumx / n;

// Compute the regression coefficient
if (abs(syy) == 0)
regressionCoefficient_ = 1;
else
regressionCoefficient_ = sxy / sqrt(sxx * syy);
}

RegressionLine::~RegressionLine()
{
}

const double RegressionLine::slope() const
{
return slope_;
}

const double RegressionLine::yIntercept() const
{
return yIntercept_;
}

const double RegressionLine::regressionCoefficient() const
{
return regressionCoefficient_;
}


int main()
{
const int count = 10;
double times[count] = {
-0.20707, 0.706672, 1.63739, 2.03117, 3.31874,
5.38201, 6.79971, 6.31814, 8.20829, 8.53994 };
double water[count] = {
-0.319029, 0.0931669, 2.17286, 2.76818, 3.56743,
4.11772, 5.52709, 7.46613, 8.7654, 9.58096 };

Points pnts;

for (int i = 0; i < count; i++)
{
pnts[times[i]] = water[i];
}

RegressionLine myLine(pnts);

cout << "Slope = " << myLine.slope() << endl;
cout << "yIntercept = " << myLine.yIntercept() << endl;
cout << "Regression Coefficient = "<< myLine.regressionCoefficient() << endl;

return 0;
}
...全文
681 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
mimengdelei 2012-05-10
  • 打赏
  • 举报
回复
#include <map>
#include <string>
#include <iostream>
#include <math.h>
using namespace std;

放到单独.hpp上面的几行加了吗?
你的错误提示都是类型未声明之类的。
liuhao159753 2008-11-14
  • 打赏
  • 举报
回复
你放在.h文件中时,在main函数中用到那个类时,需要先声明下那个类吧,然后才可以用,或是像7楼那样 RegressionLine::Points pnts;
来用。
总觉得你这提示出的错误,都是某个细节出问题了。我是新手,拙见。
Jason_Yao 2008-11-14
  • 打赏
  • 举报
回复
回7楼的朋友:
你的程序确实可以运行。但是当我把前面的类定义单独放到一个.hpp文件中的时候就不行了,出现了13个错误:
d:\ctest\linear_regression\regressionline.hpp(12) : error C2143: syntax error : missing ';' before '<'
d:\ctest\linear_regression\regressionline.hpp(12) : error C2059: syntax error : '<'
d:\ctest\linear_regression\regressionline.hpp(12) : error C2238: unexpected token(s) preceding ';'
d:\ctest\linear_regression\regressionline.hpp(13) : error C2629: unexpected 'class RegressionLine ('
d:\ctest\linear_regression\regressionline.hpp(13) : error C2238: unexpected token(s) preceding ';'
d:\ctest\linear_regression\regressionline.cpp(33) : error C2065: 'Points' : undeclared identifier
d:\ctest\linear_regression\regressionline.cpp(33) : error C2065: 'points' : undeclared identifier
d:\ctest\linear_regression\regressionline.cpp(34) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
d:\ctest\linear_regression\regressionline.cpp(105) : error C2039: 'Points' : is not a member of 'RegressionLine'
d:\ctest\linear_regression\regressionline.hpp(10) : see declaration of 'RegressionLine'
d:\ctest\linear_regression\regressionline.cpp(105) : error C2146: syntax error : missing ';' before identifier 'pnts'
d:\ctest\linear_regression\regressionline.cpp(105) : error C2065: 'pnts' : undeclared identifier
d:\ctest\linear_regression\regressionline.cpp(109) : error C2109: subscript requires array or pointer type
d:\ctest\linear_regression\regressionline.cpp(109) : error C2106: '=' : left operand must be l-value
Error executing cl.exe.

请问这是什么问题啊?谢谢
太乙 2008-11-10
  • 打赏
  • 举报
回复


#pragma warning (disable:4786)

//#include "RegressionLine.hpp"
#include <map>
#include <string>
#include <iostream>
#include <math.h>


using namespace std;



class RegressionLine
{
public:
typedef map<double,double> Points;
RegressionLine(Points & points);
virtual ~RegressionLine();

const double slope() const;
const double yIntercept() const;
const double regressionCoefficient() const;

private:

double slope_;
double yIntercept_;
double regressionCoefficient_;

};
// 上面是定义一个类,如果把它作为一个头文件,放在.hpp文件中的话,编译就会出现很多错误。而把typedef拿出来后,把这段代码贴到.cpp文件
// 的前面的话,程序就能运行了,请问这是为什么啊?

RegressionLine::RegressionLine(Points & points)
{
int n = points.size();
if (n < 2)
throw (string("Must have at least two points"));


double sumx=0,sumy=0,sumx2=0,sumy2=0,sumxy=0;
double sxx,syy,sxy;

// Conpute some things we need
map<double, double>::const_iterator i;
for (i = points.begin(); i != points.end(); i++)
{
double x = i->first;
double y = i->second;

sumx += x;
sumy += y;
sumx2 += (x * x);
sumy2 += (y * y);
sumxy += (x * y);
}
sxx = sumx2 - (sumx * sumx / n);
syy = sumy2 - (sumy * sumy / n);
sxy = sumxy - (sumx * sumy / n);

// Infinite slope_, non existant yIntercept
if (abs(sxx) == 0)
throw (string("Inifinite Slope"));

// Calculate the slope_ and yIntercept
slope_ = sxy / sxx;
yIntercept_ = sumy / n - slope_ * sumx / n;

// Compute the regression coefficient
if (abs(syy) == 0)
regressionCoefficient_ = 1;
else
regressionCoefficient_ = sxy / sqrt(sxx * syy);
}

RegressionLine::~RegressionLine()
{
}

const double RegressionLine::slope() const
{
return slope_;
}

const double RegressionLine::yIntercept() const
{
return yIntercept_;
}

const double RegressionLine::regressionCoefficient() const
{
return regressionCoefficient_;
}


int main()
{
const int count = 10;
double times[count] = {
-0.20707, 0.706672, 1.63739, 2.03117, 3.31874,
5.38201, 6.79971, 6.31814, 8.20829, 8.53994 };
double water[count] = {
-0.319029, 0.0931669, 2.17286, 2.76818, 3.56743,
4.11772, 5.52709, 7.46613, 8.7654, 9.58096 };

RegressionLine::Points pnts;

for (int i = 0; i < count; i++)
{
pnts[times[i]] = water[i];
}

RegressionLine myLine(pnts);

cout << "Slope = " << myLine.slope() << endl;
cout << "yIntercept = " << myLine.yIntercept() << endl;
cout << "Regression Coefficient = "<< myLine.regressionCoefficient() << endl;

return 0;
}


以上代码在vc6.0和mingw下都能通过:

运行结果:

vc6.0:

Slope = 1.04116
yIntercept = -0.0754263
Regression Coefficient = 0.966565
Press any key to continue


mingw下:

Slope = 1.04116
yIntercept = -0.0754263
Regression Coefficient = 0.966565


Terminated with return code 0
Press any key to continue ...








Jason_Yao 2008-11-10
  • 打赏
  • 举报
回复
一到四楼的方法,本来的程序就是这么写的。但是运行不通,编译出来20几个错误。主要是说point没定义,还有说头文件出现了不认识的“<”之类的。
我的编译环境是vc++6.0
谢谢各位
chaojiew 2008-11-08
  • 打赏
  • 举报
回复
,放在.hpp文件中的话,编译就会出现很多错误

什么错误?
liumingrong 2008-11-08
  • 打赏
  • 举报
回复
你的typedef map<double,double> Points;是在类RegressionLine作用域内,
在外面使用就得RegressionLine::Points,或者把它拿出来
xhs_lh04 2008-11-08
  • 打赏
  • 举报
回复
RegressionLine::Points pnts;
应该这样用吧,因为是类里的一个typedef

或者你把
typedef map<double,double> Points;
移到类外面试试,那样肯定可以
lzr4304061988012 2008-11-08
  • 打赏
  • 举报
回复

int main()
{...
RegressionLine::Points pnts;
...
}


baihacker 2008-11-08
  • 打赏
  • 举报
回复
RegressionLine::Points

如果放在里面,应该像上面这样使用这个Points

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧