Function outside of class

Hello, I am trying to create functions outside the class they are used in and am getting errors about duplicate creations. is this not how you create functions outside of classes? by defining them inside and then doing the work outside?




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <string>
using namespace std;

class apartment
{
private:

	string _tenantName = "Joe Smith";

public:
	void setTenantName (string);
	string getTenantName();
};




void apartment::setTenantName(string name)
{

	_tenantName = name;

}

string apartment::getTenantName()
{

	return _tenantName;

}
You should put the function definitions (line 18-30) inside a source file (.cpp). If you put it inside the header and the header is included in more than one source file you will get an error because the functions gets defined multiple times. Only inline functions are allowed to be defined more than once. Functions that are defined inside the class body are automatically inline so that was why you didn't get an error before.
To escape this error message you can define these functions as inline.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <string>
using namespace std;

class apartment
{
private:

	string _tenantName = "Joe Smith";

public:
	void setTenantName (string);
	string getTenantName();
};




inline void apartment::setTenantName(string name)
{

	_tenantName = name;

}

inline string apartment::getTenantName()
{

	return _tenantName;

}


EDIT: By the way it is better to declare getTenantName as

string getTenantName() const;
Last edited on
Your only mistake is:
 
string _tenantName = "Joe Smith";

you cannot define an instance variable inside a class, u call a constructor for that.
@vlad from moscow:
usage of inline functions is something different, inline functions are expanded at the place of call so the flow is faster, its used in some rare cases of optimsation.

please go though it before u suggest blindly.

even ur code will throw up compilation error because of above mentioned reason.
C++ now allows in-class member initializers.
http://www.stroustrup.com/C++11FAQ.html#member-init

@anirudh sn
@vlad from moscow:
usage of inline functions is something different, inline functions are expanded at the place of call so the flow is faster, its used in some rare cases of optimsation.

please go though it before u suggest blindly


"is something different" with what? I doubt that you understood my post. To escape the error of breaking the ODR-rule you can either define a function in a class as Peter87 pointed out that to make it inline or define it outside the class but specifying inline function specifier. In this case there is no need to modify rest modules of the project.

So I do not see any great sense in your improper remark "please go though it before u suggest blindly".
This remark has more sense apllied to you yourself.

Also I advice you to read new C++ Standard about data class member definitions.
Last edited on
thank u, good to know, another bull shit optimization, the compiler puts that automatically in the constructor, ISO guys can pat their their backs for this, then wats wrong with the code by blucky ???
> another bull shit optimization, the compiler puts that automatically in the constructor, ISO guys...

Who are you? There used to be a resident Java bigot sometime back, whose diatribes were characterised by fanaticism and ignorance, in roughly equal measure. If you are the same person, under a pseudonym now, may I direct you to the "lounge" section. You may have admires there, people who relish your fulminations.
Regardless of the spat over use of inline, Peter87 is correct - unless there's an overriding reason not to, you should put the implementation of your methods into a .cpp file that is compiled once and then linked by other translation units.

While there are sometimes reasons to include the complete method implementation in the header file, it's usually best not to. Yes, I'm keeping it simple for a beginner.
Topic archived. No new replies allowed.