#include <iostream>
#include <string>
using namespace std;
class Port{
public:
string Name( string x);
};
int main(){
Port Object;
Object.Name( "sword of ceaser");
cout << Object.Name();
}
Asked
Active
Viewed 62 times
-4

Borgleader
- 15,826
- 5
- 46
- 62
-
Where is the implementation of the member function Name()? – CuriouslyRecurringThoughts Jun 23 '19 at 11:58
-
How do you run this program? It can't be compiled. – Thomas Sablik Jun 23 '19 at 12:03
-
5`I get error whenever I run this program C++` You forgot to say what error that was. We can't just guess... – Borgleader Jun 23 '19 at 12:04
-
What are you trying to achieve with this code? Do you really want a method `Name` or do you want a member `Name`? – Thomas Sablik Jun 23 '19 at 12:08
-
@Borgleader “Candidates expects 1 arguments , 0 provided “ – Jun 23 '19 at 12:09
-
1You can't call `Object.Name()` without argument. You can overload the method `Name` with `void Name( string x);` and `string Name( );` – Thomas Sablik Jun 23 '19 at 12:10
-
@ThomasSablik I tried to create a function and pass some string parameters, then call it in main with a string argument and have the output of whatever string argument I had given it. I might be wrong but I just started c++, so forgive my lack of knowledge. – Jun 23 '19 at 12:11
1 Answers
3
First you need an implementation of the method (function) Name
. How can the compiler know what to do if you don't tell. You can overload the method Name
with void Name( string x)
and string Name( )
.
Next you need a member to store this value.
#include <iostream>
#include <string>
class Port{
public:
void Name(std::string x) {
name = x;
}
std::string Name() {
return name;
}
private:
std::string name;
};
int main(){
Port Object;
Object.Name( "sword of ceaser");
std::cout << Object.Name();
}
You should avoid using namespace std;
. It's a bad habit.

Thomas Sablik
- 16,127
- 7
- 34
- 62
-
Could you explain more to me about using namespace std; About it being a bad habit, I’m new to it. And it looks easier to me – Jun 23 '19 at 12:18
-
5@user11688094 See this question: [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Yksisarvinen Jun 23 '19 at 12:26