0
OOP
I wrote classes and I want to use it one class But my code is not working Where is the problem? Here is my code https://ideone.com/Mhd9uf
3 Respostas
+ 3
( 2 of 2 )
Lastly:
prog.cpp:163:11: error: use of deleted function ‘Route::Route()’
Route obj;
^~~
prog.cpp:101:7: note: ‘Route::Route()’ is implicitly deleted because the default definition would be ill-formed:
class Route{
As it's trying to tell you, Route cannot be probably constructed because your Route class contains member variables that don't have a default constructor themselves.
For example:
passenger student;
Which is a member of Route, only has a constructor that takes 4 parameters.
This member has to be constructed when Route is constructed.
But you never provided the 4 parameters necessary to construct passenger.
The compiler cannot guess what the parameters should be.
Solution:
Either add a default constructor for each class, or define your own constructor in Route that initializes each member in it's class.
The later is probably not possible in this case, so the former is probably a better solution.
On another note:
Capitalize all your classes, stay consistent.
You use c and d for conductor and driver, respectively.
Probably a good idea to give them meaningful names?
Post a link to the sololearn's playground if you can. ( for next time )
Also I did not look at code logic.
+ 1
( 1 of 2 )
Did you read the compilation info at the bottom of the page?
It clearly states all problems in your code.
prog.cpp:116:37: error: no match for call to ‘(passenger) (int&, std::__cxx11::string&, std::__cxx11::string&, bool&)’
student(money,from,to,health);
Line 116:
void input()
{
...
student(money,from,to,health);
...
}
It seems that you're trying to call student's constructor.
You can't construct an object again after it's constructed already.
You can fix this by calling the appropriate setters instead.
The errors repeat for:
line 119: c(health,money);
line 122: d(health,safety);
Next error:
prog.cpp:129:36: error: request for member ‘size’ in ‘((Route*)this)->Route::BusStop’, which is of non-class type ‘std::__cxx11::string [7]’ {aka ‘std::__cxx11::basic_string<char> [7]’}
for(int i=0; i<BusStop.size(); i++)
Definition of BusStop:
string BusStop[7]={ ... };
BusStop is a raw array, it does not have any member functions.
Recommending an std::vector<std::string>, or an std::array<std::string, 7>.
The later one is recommended in this case since you're not adding new strings anyway. Might make it static (const) in this case as well, since you're not modifying it either.
Next error:
prog.cpp:135:29: error: ‘bool passenger::inBus’ is private within this context
student.inBus();
You have a member variable:
bool inBus=false;
and a member function:
void InBus()
{
inBus=1;
}
Not the capital I.
Recommending to prefix your member variables with d_ or with m_ so that you don't have to mix capitalized functions with non-capitalized functions.
Prefixing these functions with set/get is also an idea, especially the set is recommended.
Next error:
prog.cpp:151:33: error: ‘bool passenger::inBus’ is private within this context
else if(student.inBus()&&BusStop[i]==student.To())
Changing this to InBus won't solve it, because it's a setter.
Your getter is called 'state' for some reason.