Reputation: 35
I am a former c programmer and have spent the last few years working in python. I am now attempting to understand c++ object programming. I have created a class which contains a structure. I would like to pass a reference to the class to a function. I have read several articles about passing by reference like the following:
passing object by reference in C++
Is it possible to pass a class reference to a function or is this something that can't be done in c++?
I am attempting to pass a reference to a class to a print printStruct function. Here is my example:
#include "stdafx.h"
#include <string>
#include <iostream>
class SampleClass
{
public:
struct TestStructType {
std::string string1;
};
SampleClass();
std::string GetString1();
private:
TestStructType PrivateVar;
};
SampleClass::SampleClass()
{
PrivateVar.string1 = "String 1";
};
std::string SampleClass::GetString1() { return PrivateVar.string1; };
void printStruct(SampleClass&);
int _tmain(int argc, _TCHAR* argv[])
{
SampleClass sc;
std::cout << sc.GetString1() << std::endl;
printStruct(sc);
return 0;
};
void printStruct(SampleClass& ssc)
{
std::cout << "The String is: " << ssc.GetString1() << std::endl;
};
Error messages: Error 1 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\visual studio 2013\projects\pass class\pass class\pass class.cpp 8 1 Pass Class
Error 2 error C2143: syntax error : missing ',' before '&' c:\users\visual studio 2013\projects\pass class\pass class\pass class.cpp 8 1 Pass Class
5 IntelliSense: the object has type qualifiers that are not compatible with the member function object type is: const SampleClass c:\Users\Visual Studio 2013\Projects\Pass Class\Pass Class\Pass Class.cpp 41 37 Pass Class
Upvotes: 3
Views: 12274
Reputation: 20063
There are two problems here. The first one is you are passing a pointer not a reference or object instance. You can fix this by either dereferencing the pointer when you pass it as an argument or just pass sc
instead of psc
.
SampleClass sc;
SampleClass *psc = &sc
printStruct(*psc);
// ^ - dereference the pointer.
or
SampleClass sc;
printStruct(sc);
The second problem is you are calling a non const-qualified member function on a const-qualified instance. Since GetString1
does not modify any member variables you can easily fix this by declaring it const
.
class SampleClass
{
std::string GetString1() const;
};
std::string SampleClass::GetString1() const
{
return PrivateVar.string1;
}
Upvotes: 3
Reputation: 4561
Update You mentioned it doesn't compile due to const. That is because you declared the reference as a const reference and GetString1 is not const. See end of this answer for options.
Using references and pointers are similar in that they refer to the address of an actual object. There are some differences, for example a reference cannot refer to (will segfault) a nullptr nor be left undefined (meaning a reference must be assigned at the point of its construction).
When you do not want to copy the object it is common to pass by address (pointer), or by reference (generally people prefer pointers). Though the two types behave similarly under-the-hood, the syntax for the two is separate. Your method declaration:
void printStruct(const SampleClass&);
Expects a reference, and you are passing a pointer.
SampleClass * psc;
You have two options, either expect a pointer or pass an object (whose reference will be used).
example one, passing a reference:
printStruct(sc);
example two, passing a pointer:
void printStruct(const SampleClass*);
// in main
SampleClass* psc = ≻
printStruct(psc);
Issue with const
In order to call methods on these arguments, those methods must be const (or remove the const specifier on the parameter type)
example one, remove const
void printStruct(SampleClass*);
or
void printStruct(SampleClass&);
example two, add const
class SampleClass
{
public:
std::string GetString1() const;
};
Upvotes: 0