I am trying to compile this c++ program but getting this error can somone help m
ID: 3899324 • Letter: I
Question
I am trying to compile this c++ program but getting this error can somone help me ?
#include<iostream>
#include<string>
#include <iomanip>
#include<sstream>
#include<string.h>
#include<stdlib.h>
using namespace std;
class Runway
{
public:
Runway(std::string s);
std::string site_number(void) const;
std::string name(void) const;
int length(void) const;
private:
int convert_length(std::string s) const;
const std::string site_number_;
const std::string name_;
const int length_;
};
Runway::Runway(string s){
char *ch = strtok((char *)s.c_str()," ");
const string site = ch;
const string name = strtok(NULL," ");
const int length = atoi(strtok(NULL," "));
site_number_ = site;
name_ = name;
length_ = length;
}
string Runway::site_number(void)const{
return site_number_;
}
string Runway::name(void)const{
return name_;
}
int Runway::length(void)const{
return length_;
}
int Runway::convert_length(string s) const{
return atoi((char *)s.c_str());
}
/*testRunway.cpp*/
int main()
{
string line;
getline(cin,line);
Runway r(line);
cout << r.site_number() << " " << r.name() << " "
<< setw(8) << r.length() << endl;
}
Runway: :Runway (string s)t Constructor for 'Runway' must explicitly initialize the const member 'length' . . . . char-*ch-=, strtok( (char-*)s.c-str()," ."); ..const, string-site = ch; .const string namestrtok (NULL,"); , const, int length-=-atol (strtok (NULL' " " " ) ),- site numbersite; name name: length length; No viable overloaded ' ! No viable overloaded'' 0 I 9 0 Cannot assign to non-static data member 'length_' with const-qualified type 'const int' string Runway::site number (void)consti return site_number- string Runway::name (void)const return.name int Runway: :length(void)constt .return length_i- int Runwav: :convert length(string ) const4-Explanation / Answer
Replace your constructor with following constructor:
Runway::Runway(string s) : length_(atoi(strtok(NULL," "))), site_number_(strtok((char *)s.c_str()," ")), name_(strtok(NULL," ")){}
Modified Code:
#include<iostream>
#include<string>
#include <iomanip>
#include<sstream>
#include<string.h>
#include<stdlib.h>
using namespace std;
class Runway
{
public:
Runway(std::string s);
std::string site_number(void) const;
std::string name(void) const;
int length(void) const;
private:
int convert_length(std::string s) const;
const std::string site_number_;
const std::string name_;
const int length_;
};
Runway::Runway(string s) : length_(atoi(strtok(NULL," "))), site_number_(strtok((char *)s.c_str()," ")), name_(strtok(NULL," ")){}
string Runway::site_number(void)const{
return site_number_;
}
string Runway::name(void)const{
return name_;
}
int Runway::length(void)const{
return length_;
}
int Runway::convert_length(string s) const{
return atoi((char *)s.c_str());
}
/*testRunway.cpp*/
int main()
{
string line;
getline(cin,line);
Runway r(line);
cout << r.site_number() << " " << r.name() << " "<< setw(8) << r.length() << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.