0
votes

I've been getting errors all day when I tried to use return reference or pointer in c++. Please tell me what should I change in code below to return Config class members by reference or pointer.

#include <cstdlib>
#include <iostream>

using namespace std;

class JustExample {
    public:
        string someText;
        JustExample() {
              someText = "blablabla";
        }
};

class Config
{
    public:
        Config();
        string getResourceDir() const;
        JustExample getExmp() { return exmp; }
        void setResourceDir(const string& dir);
        void setExmp(JustExample example) { exmp = example; }
    private:
        JustExample exmp;
        string res_dir;
};

Config::Config() { 
    res_dir = "class value";
}
string Config::getResourceDir() const { return res_dir; }
void Config::setResourceDir(const string& dir) { res_dir = dir; }

int main(int argc, char *argv[])
{
    Config cfg;
    cout << cfg.getResourceDir() << endl;
    cfg.setResourceDir("changed");
    cout << cfg.getResourceDir() << endl;

    cout << cfg.getExmp().someText << endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}

Bonus question. Why can't I initialize variables after their declaration but have to assign their value in class constructor instead or I get an error.

8 D:\projects\dev-cpp test\main.cpp ISO C++ forbids initialization of member `someText' 
8 D:\projects\dev-cpp test\main.cpp making `someText' static 
8 D:\projects\dev-cpp test\main.cpp invalid in-class initialization of static data member of non-integral type `std::string' 
 D:\projects\dev-cpp test\main.cpp In function `int main(int, char**)': 
38 D:\projects\dev-cpp test\main.cpp 'class JustExample' has no member named 'someText' 
 D:\projects\dev-cpp test\Makefile.win [Build Error]  [main.o] Error 1 
2
"Why can't I initialize variables after their declaration?" -- because C didn't allow that for struct members, and C++ didn't add it until much much later. Even initialization of static integral members wasn't available until C++03, and the brace-or-initializer-list for non-static members needs at least C++11, which your compiler may only partially support. - Ben Voigt
Does it not work to do this? JustExample& /* << ADDED THERE */ getExmp() { return exmp; } ? - Ben Voigt
I'm assuming that those errormessages come from code you didn't show which has string someText = "blablabla"; as a class member ? - M.M
You should really post two separate questions as the problem with JustExample is entirely different to the question about Config's getter/setters - M.M
@MattMcNabb: Yeah you are probably right but using visual studio is a pain for me especially since I'm just starting with c++. I wrote this quick snippet in Dev C++ just to quickly demonstrate what gives me a little headache and encountered a problem with initialization that threw me off a bit. ~Thanks - sebastian_t

2 Answers

3
votes

The code you provide compiles with clang on mac OS X with no error. But I don't see where do you return reference or pointer, in Config you can do this:

class Config
{
    public:
        Config();
        const string& getResourceDir() const;
        const JustExample& getExmp() { return exmp; }
        void setResourceDir(const string& dir);
        void setExmp(const JustExample& example) { exmp = example; }
    private:
        JustExample exmp;
        string res_dir;
};

return by pointer is similar, just replace & with *, although it may be better to return reference in this case.

1
votes

Your code for the getter and setter is correct, however, you forgot #include <string>. No other changes are required.

For the second question. You need to use C++11 to get in-line initialization of the string; but in C++03 you should use the constructor initializer list:

JustExample():  someText("blablabla")
{
}