3

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:

CORRECTED CODE

#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

Community
  • 1
  • 1
user3784804
  • 35
  • 1
  • 1
  • 8
  • What problem are you having while doing this? Seems to me like you're looking for http://stackoverflow.com/questions/57483/what-are-the-differences-between-pointer-variable-and-reference-variable-in-c. – chris Jul 01 '14 at 00:36
  • Well, the class hasn't been declared at that point. That's the same as in C. – chris Jul 01 '14 at 00:39
  • Please edit your post and include the exact error message there instead of a comment. – Captain Obvlious Jul 01 '14 at 00:39
  • `printStruct(psc);` should be simply `printStruct(sc);`. You don't need a pointer. – πάντα ῥεῖ Jul 01 '14 at 00:41
  • Yes. There is an error the line where the pointer to the class is declared. It should read as follows: SampleClass * psc = &sc; – user3784804 Jul 01 '14 at 00:42
  • Error 4 error C2662: 'std::string SampleClass::GetString1(void)' : cannot convert 'this' pointer from 'const SampleClass' to 'SampleClass &' c:\users\visual studio 2013\projects\pass class\pass class\pass class.cpp 41 1 Pass Class – user3784804 Jul 01 '14 at 00:50
  • @user3784804 You should not edit the corrections into your question; leave the question as-is, and accept an answer. If you really want to post more stuff then do it after the end of the existing text. Otherwise it is confusing to anyone reading this question for the first time. – M.M Jul 01 '14 at 01:49

2 Answers2

3

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;
}
Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
  • This does not compile under visual studio. See error message above. – user3784804 Jul 01 '14 at 00:53
  • You're a genius. Thanks for catching const problem. That is exactly what I needed. – user3784804 Jul 01 '14 at 01:08
  • Is one solution better than the other? both solutions work. Removing the const keyword in the function declaration or appending the const keyword to the function declaration like so: std::string GetString1() const; Thanks again. – user3784804 Jul 01 '14 at 01:21
  • It is more idiomatic to declare member functions `const` if they are not intended to modify the object they are invoked on. In this case declaring it `const` is exactly what you want to do. – Captain Obvlious Jul 01 '14 at 01:28
0

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 = &sc;
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;
};
payo
  • 4,501
  • 1
  • 24
  • 32