-1

I have objects that I put into a std::vector. Later on I need to iterate through the vector and change some member variables in the objects in each position.

I think I want to pass the object once I have it by reference to a function to operate on it, but I seem to be getting an error:

Non-const lvalue reference to type 'Object' cannot bind to a value of unrelated type 'Object *'

Here is the general gist with code between omitted:

Object* o1 = Object::createWithLocation(p.x, p.y);
v.push_back(o1); 

// later on
for (int f=0; f < v.size(); f++)
{
    Object* obj1 = v.at(f);

    addChild(h->createLayer(obj1), 3); // add the HUD
}

createLayer is defined at:

static PlantingHUD* createLayer(Object &o);    

Can anyone explain my confusion between pointers and passing by reference? Do I have to do a cast of some sort?

Jasmine
  • 15,375
  • 10
  • 30
  • 48
  • 2
    Did you read the error message? Did you have a look at the type of your function argument and the type of the variable you actually pass in? It's so trivial... –  Jul 28 '13 at 05:28
  • I did and I my thought was to use Object rather than Object* but the vector is defined as: `std::vector v;` – Jasmine Jul 28 '13 at 05:42
  • 1
    And what did the beginners' C++ guide say about converting an `Object *` to an `Object`? Of course you use the pointer dereference operator (`*`) described in Chapter One (or two)... –  Jul 28 '13 at 05:45
  • Let me go back and look, Thanks for the reminder about dereferencing. I bet that is what I am failing to do. – Jasmine Jul 28 '13 at 05:49

2 Answers2

0
static PlantingHUD* createLayer(Object &o);

this method need a reference to Object as the parameter, but your input is a pointer.

Object* obj1 = v.at(f);

addChild(h->createLayer(obj1), 3); // add the HUD

That's the problem.

hack4m
  • 285
  • 2
  • 5
  • 11
0
void foo(Object o)

Declares a function, foo, which will begin execution with a fresh, new, instance of class 'Object' called 'o'.

This is called "passing by value", but it's more accurately 'copying' because what foo receives is it's own, personal copy of the Object instances we call foo with. When "foo" ends, the "Object o" it knew, fed and put through school, will cease to be.

void foo(Object& o)

Declares a function, foo, which will begin executing with a reference to an existing instance of an 'Object', this reference will be called 'o'. If you poke or prod it, you will be changing the original.

This is called "pass by reference".

void foo(Object* o)

Declares a function, foo, which will begin executing with a variable, called "o", containing the address of what is supposed to be an instance of "Object". If you change this variable, by doing something like "o = nullptr", it will only affect the way things look inside foo. But if you send Samuel L Jackson to the address, he can deliver furious vengance that lasts beyond the lifetime of foo.

void foo(Object*& o)

Declares a function, foo, which will begin executing with a variable called "o", which is a reference to a pointer to an instance of object o - it's like an alias, except that without compiler optimization, it's actually implemented by the compiler using a sort of pointer.

Lets try these separately.

#include <iostream>
#include <cstdint>

struct Object
{
    int m_i;

    void event(const char* what, const char* where)
    {
        std::cout <<
            what<< " " << (void*)this <<
            " value " << m_i <<
            " via " << where <<
            std::endl;
    }

    // Construct an object with a specific value.
    Object(int i) : m_i(i)
    {
        event("Constructed", "Operator(int i)");
    }

    // This is called the copy constructor, create one object from another.
    Object(const Object& rhs) : m_i(rhs.m_i)
    {
        event("Constructed", "Operator(const Object&)");
    }

    // This is how to handle Object o1, o2; o1 = o2;
    Object& operator=(const Object& rhs)
    {
        m_i = rhs.m_i;
        event("Assigned", "operator=");
        return *this;
    }

    // Handle destruction of an instance.
    ~Object() { event("Destructed", "~Object"); }
};

void foo1(Object o)
{
    std::cout << "Entered foo1, my o has value " << o.m_i << std::endl;
    // poke our local o
    o.m_i += 42;
    std::cout << "I changed o.m_i, it is " << o.m_i << std::endl;
}

void foo2(Object* o)
{
    std::cout << "Foo2 starts with a pointer, it's value is " << (uintptr_t)o << std::endl;
    std::cout << "That's an address: " << (void*)o << std::endl;
    std::cout << "m_i of o has the value " << o->m_i << std::endl;
    o->m_i += 42;
    std::cout << "I've changed it tho, now it's " << o->m_i << std::endl;
}

void foo3(Object& o)
{
    std::cout << "foo3 begins with a reference called o, " << std::endl <<
        "which is sort of like a pointer but the compiler does some magic " << std::endl <<
        "and we can use it like a local concrete object. " <<
        std::endl <<
        "Right now o.m_i is " << o.m_i <<
        std::endl;
    o.m_i += 42;
    std::cout << "Only now, it is " << o.m_i << std::endl;
}

void foo4(Object*& o)
{
    std::cout << "foo4 begins with a reference to a pointer, " << std::endl <<
        "the pointer has the value " << (uintptr_t)o << " which is " <<
        (void*)o <<
        std::endl <<
        "But the pointer points to an Object with m_i of " << o->m_i << std::endl <<
        "which we accessed with '->' because the reference is to a pointer, " <<
        "not to an Object." <<
        std::endl;
    o->m_i += 42;
    std::cout << "I poked o's m_i and now it is " << o->m_i << std::endl;
    // Now for something really dastardly.
    o = new Object(999);
    std::cout << "I just changed the local o to point to a new object, " <<
        (uintptr_t)o << " or " << (void*)o << " with m_i " << o->m_i <<
        std::endl;
}

int main()
{
    std::cout << "Creating our first objects." << std::endl;
    Object o1(100), o2(200);

    std::cout << "Calling foo1 with o1" << std::endl;
    foo1(o1);
    std::cout << "back in main, o1.m_i is " << o1.m_i << std::endl;

    std::cout << "Calling foo2 with &o1" << std::endl;
    foo2(&o1);
    std::cout << "back in main, o1.m_i is " << o1.m_i << std::endl;

    std::cout << "Calling foo3(o2), which looks like the way we called foo1." << std::endl;
    foo3(o2);
    std::cout << "back in main, o2.m_i is " << o2.m_i << std::endl;

    std::cout << "Creating our pointer." << std::endl;
    Object* optr;
    std::cout << "Setting it to point to 'o2'" << std::endl;
    optr = &o2;
    std::cout << "optr now has the value " << (uintptr_t)optr <<
        " which is the address " << (void*)optr <<
        " which points to an Object with m_i = " << optr->m_i <<
       std::endl;

    foo4(optr);

    std::cout << "back in main, o2 has the value " << o2.m_i << std::endl <<
        "and now optr has the value " << (uintptr_t)optr << std::endl <<
        "and optr->m_i is now " << optr->m_i <<
        std::endl;

    if (optr != &o2)
        delete optr; // otherwise we'd technically be leaking memory.

    return 0;
}

Live demo on ideone.com.

Passing by Value

This term confuses people early in their C++ development because, in lay terms, it sounds like this is what "Object& foo" would do.

The term "pass by value" actually arises from what the language has to do to call such a function, to value-wise copy the whole of the original object/struct onto the stack or, in the case where a copy ctor is available, forward them to a value-wise constructor and recreate a copy of the original, value-by-value.

Pass-by-value should be used for most simple cases where you do not want side-effects on the values in your current scope from the function you are calling.

bool checkWidthdrawl(Dollars balance, Dollars amountToWithdraw)
{
    // it's safe for me to change "balance" here because balance is mine
}

vs

bool checkWidthdrawl(Dollars& balance, Dollars amountToWithdraw)
{
    balance -= amountToWithdraw;
    if (balance < 0)
        std::complaint << "My account seems to be missing $" << amountToWithdraw;
}

However, passing by reference can become expensive.

struct FourK { char a[1024], b[1024], c[1024], d[1024]; }

If you pass this around by value all day, you risk blowing up your stack at some point, as well as spending daft amounts of time copying all those bytes.

void foo(int i); // Unless you need to see the changes to i, this is perfectly fine.
void foo(FourK f); // Someone should hunt you down and yell "PEANUT" in your ear.

Passing by reference

References are really a contract over the pointer system that allow the language to ensure you're really talking about a concrete instance of an object, and thus allow you to refer to a pre-existing instance of a value outside of a function.

Of course, there are ways to break this, but the language tries very, very hard to make them difficult to do. For example, try adding this to the above code:

Object& makeObjectNotWar(int i)
{
    Object thisObjectGoesAway(i);
    return thisObjectGoesAway /*right about now*/;
}

You can also provide callers with an assurance that the function won't have any side effects on a variable with the "const" modifier.

void fooc(const Object& o)
{
    o.m_i += 42; // Error
}

You can even use that within a function as a hint to yourself (and the compiler) that you don't want to accidentally change a value, here's a case where it can provide an optimization hint to the compiler:

std::vector<int> foo;
add1000valuesTo(foo);
const size_t fooSize = foo.size();
for (size_t i = 0; i < fooSize; ++i) {
    // ... stuff you're sure won't decrease foo.size()
}

Without the const fooSize

for (size_t i = 0; i < foo.size(); ++i) {

The compiler has to start by assuming that "foo.size()" could be changed at any given iteration of the loop. It can probably figure out that it doesn't, but by giving it the hint, you've saved a little compile time, possibly improved your performance, and made it easier for a human to tell exactly what behavior you expected. Downside: If your loop does actually change the size of foo, you'll find out by bug reports :(

One last thing to know about pass-by-reference is that C++ references aren't protected or "ref counted". The language only promises that a reference will be valid for the duration of its scope, so long as you don't do anything stupid like, say, call something that deletes the object.

// Author intended this function to be called
// by the owner of a Dog.
void doneWithFoo(Dog& dog)
{
    Dog* deadDog = &dog;
    delete deadDog;
}

Rover& Babysitter::babysitDog(Dog& rover, int hours)
{
    rover.feed(FeedType::Donut);
    if (rover.pooped())
       doneWithDog(rover);
    // ...
    return rover; // I have a bad feeling about this.
}

Obviously, you're not expecting "babysitDog" to result in the dog being disposed of. But bear in mind that because we passed in a reference, it to "babysitDog" that it's also gone from the caller too, and if that was using a reference... rover's dead, Dave, dead.

As with pointers, if you're going to store references beyond the scope in which you have access to them, then you become responsible for making sure the objects being referenced stick around or that the references are removed from the container before the objects do go away.

kfsone
  • 23,617
  • 2
  • 42
  • 74
  • Wow, I want to run this, but I am getting some undefined symbols. I am using GCC-4.8 on OS X. `$ gcc-4.8 -W -Wall -Wextra -pedantic -std=c++0x -o object object.cpp` and getting some undefined symbols for x86_64/: ` std::basic_ostream >::operator<<(std::basic_ostream >& (*)(std::basic_ostream >&)), referenced from` – Jasmine Jul 28 '13 at 15:21
  • Erm - it's C++ code, you want to use g++-4.8 not gcc-4.8. It compiles without problem for me under Mint 15 with: `osmith@olivia64 ~ $ g++-4.8 -O0 -g3 -Wall -Wextra -pedantic -std=c++11 -o object object.cpp` (note: -W = -Wall so you don't need both) – kfsone Jul 28 '13 at 18:55
  • Added an extra section to the end of the answer. – kfsone Jul 28 '13 at 19:53