-4

Is it possible to edit an object that has not yet been defined in a function? For example, if I had a class in one file that adds one to a number:

class MyClass
{
    private:
       int i;

    public:
        MyClass()
        {
            i = 0;
        }

        int addOne()
        {
            i += 1;
            return i;
        }
};

And in another file, the class is imported. When the add function is called it calls the addOne function from the class:

#include "MyClass.h"

void add()
{
    a.addOne();
}

int main()
{
    MyClass a;
    add();

    return 0;
}

When I compile this it returns the error

error: ‘a’ was not declared in this scope

Is there any way for this to work?

Sites I read but didn't help solve my problem:

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
T. Green
  • 323
  • 10
  • 20
  • 3
    Why would you want / expect it to work? – iBug Feb 05 '18 at 10:46
  • 4
    You have to pass `a` to `add()` e.g. `void add(MyClass &a) { a.addOne(); }` and call it in `main()` with `add(a);`. – Scheff's Cat Feb 05 '18 at 10:47
  • 5
    do you understand what "declared in this scope" means? That's a pretty important pre-requisite to programming – Caleth Feb 05 '18 at 10:48
  • do you know what `#include` is used for? – 463035818_is_not_an_ai Feb 05 '18 at 10:48
  • did you search for your error message and try to understand it? – UKMonkey Feb 05 '18 at 10:49
  • Would the down-votes care to explain please? I find that a reasonable and carefully crafted question. – Micha Wiedenmann Feb 05 '18 at 10:49
  • @MichaWiedenmann as I said in my comment - lack of research effort – UKMonkey Feb 05 '18 at 10:50
  • @UKMonkey To me the question is: "Is there an alternative for X?" (I tried X, it gave me an error.) OP is interested in the alternative not the error. The question is not how can I resolve this error but to understand first principles, IMHO. In fact that OP tried to first work it out herself is noble, again IMHO. – Micha Wiedenmann Feb 05 '18 at 10:53
  • @UKMonkey I actually did a lot of research but I couldn't find an answer that I could understand or would work for me (I'm very new to C++). I tried looking into forward referencing as I thought that could solve my problem but nothing helped. – T. Green Feb 05 '18 at 10:55
  • @T.Green Then it's important you state in your question what you found and tried; else how do we know which part you didn't understand? – UKMonkey Feb 05 '18 at 10:56
  • You may want to look at [scope](http://en.cppreference.com/w/cpp/language/scope) – Caleth Feb 05 '18 at 11:28
  • I would also suggest looking up function arguments and references. If you're lacking a resource that can tell you what these are (for example you're not understanding the websites) then I'd suggest looking at [this](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) page to help you with that – UKMonkey Feb 05 '18 at 11:31

1 Answers1

2

What you want to do is something like this :

void add( MyClass & a)
{
    int num = a.addOne();
}

int main()
{
    MyClass newObj;
    // passing the new object to add
    add(newObj);

    return 0;
}
  1. The method add() has no idea about the "a" which you were trying to use and was therefore giving you an error.
  2. The method addOne() has a return type of an int, so it is expected that when you use a method that returns a value, then you do something with it.
Vishaal Shankar
  • 1,648
  • 14
  • 26