user3353935
user3353935

Reputation: 1

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

#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

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

#include <cstdlib>
#include <iostream>
#include "Site.h"

using namespace std;

int main (void)
{
    Site _SiteList;
    _SiteList.readSitesFromFile();

Upvotes: 0

Views: 109

Answers (3)

Sam Cristall
Sam Cristall

Reputation: 4387

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

Also, your getSiteList prototype doesn't have a return type. It appears to be Site* from your implementation, however your implementation is also wrong. It should be: Site* Site::getSiteList() instead of Site::Site* getSiteList()

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 311186

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.

Also it seems that function getSiteList() should be declared as

 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.

Upvotes: 0

Gh.
Gh.

Reputation: 101

I noticed two things about the code to begin with... Your class is called "Site" yet your default constructor is "SiteList()" and C string library should be defined as

Upvotes: 0

Related Questions