[英]Receiving an error when compiling a c++ code
I wrote this little program but getting an error, any ideas what might be wrong: 我写了这个小程序,但得到一个错误,任何想法可能是错的:
Site.h Site.h
#include <iostream>
#include <string>
using namespace std;
class Site{
private:
int ID;
string Name;
string Status;
string RemediationStatus;
string TypeofContamination;
int XCoordinate;
int YCoordinate;
public:
void readSitesFromFile();
int getSitesCount();
void showInformationAboutSiteFromFile();
void addNewSite();
void showSiteStatus();
void saveToFile();
SiteList();
getSiteList();
};
SiteList.cpp SiteList.cpp
include <iostream>
#include <string>
#include <fstream>
#include <math.h>
#include "Site.h"
using namespace std;
Site::SiteList()
{
countForSites=0;
}
Site::Site* getSiteList()
{
return sites;
}
//read Sites From File
void Site::readSitesFromFile()
{
string line;
ifstream myfile ("sites.csv");//open file sites.csv
int countofline=0;
//read from file
if (myfile.is_open())
{
while (myfile.good())
{
getline (myfile,line);//read each lines
char* cstr = new char [line.size()+1];
strcpy(cstr, line.c_str());//convert string to char*
char* pch= strtok(cstr,",");//split string using delimeter ","
int count=0;
while (pch != NULL)
{
//read ID from file sites.csv
if(count==0){
sites[countForSites].ID=atoi(pch);
}
//read Name from file sites.csv
if(count==1){
sites[countForSites].Name=pch;
}
//read Status from file sites.csv
if(count==2){
sites[countForSites].Status=pch;
}
//read RemediationStatus from file sites.csv
if(count==3){
sites[countForSites].RemediationStatus=pch;
}
//read TypeofContamination from file sites.csv
if(count==4){
sites[countForSites].TypeofContamination=pch;
}
//read XCoordinate from file sites.csv
if(count==5){
sites[countForSites].XCoordinate=atoi(pch);
}
//read YCoordinate from file sites.csv
if(count==6){
sites[countForSites].YCoordinate=atoi(pch);
}
count++;
pch = strtok (NULL, ",");
}
countForSites++;
}
myfile.close();//close file
}else{
cout << "Unable to open file\n\n";
}
}
When I run this program, I get this error: 当我运行此程序时,我收到此错误:
./Site.h:22:8: error: C++ requires a type specifier for all declarations
SiteList();
It looks like this line is not working: 看起来这条线不起作用:
Site _SiteList;
_SiteList.readSitesFromFile();
any ideas what I am doing wrong here? 我在这里做错了什么想法? main.cpp
main.cpp中
#include <cstdlib>
#include <iostream>
#include "Site.h"
using namespace std;
int main (void)
{
Site _SiteList;
_SiteList.readSitesFromFile();
Your constructor name doesn't match the class name. 您的构造函数名称与类名称不匹配。 In your
Site
class change SiteList
to Site
, then also change Site::SiteList
to Site::Site
at the top of SiteList.cpp 在
Site
类SiteList
更改为Site
,然后将Site::SiteList
更改为SiteList.cpp顶部的Site::Site
Also, your getSiteList
prototype doesn't have a return type. 此外,您的
getSiteList
原型没有返回类型。 It appears to be Site*
from your implementation, however your implementation is also wrong. 它似乎是您实现的
Site*
,但是您的实现也是错误的。 It should be: Site* Site::getSiteList()
instead of Site::Site* getSiteList()
它应该是:
Site* Site::getSiteList()
而不是Site::Site* getSiteList()
我注意到有两个关于代码开头的事情......你的类被称为“Site”,但你的默认构造函数是“SiteList()”,C字符串库应定义为
In the header you declared member functions that have no return type 在标头中,您声明了没有返回类型的成员函数
SiteList();
getSiteList();
It seems that you mean Site()
instead of SiteList()
that is this member function has to be a class constructor. 看来你的意思是
Site()
而不是SiteList()
,这个成员函数必须是一个类构造函数。
Also it seems that function getSiteList()
should be declared as 似乎函数
getSiteList()
也应声明为
Site * getSiteList();
Also this function defined incorrectly. 此功能也定义不正确。 At least it should be defined as
至少它应该被定义为
Site * Site::getSiteList()
{
return sites;
}
instead of 代替
Site::Site* getSiteList()
{
return sites;
}
Also variable sites was not declared. 还没有声明可变站点。 So it is not clear what the function actually returns.
所以不清楚函数实际返回的是什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.