-1

First a disclaimer. I've been reading the multitude of questions on this topic, yet I can't get mine to work for me. Mostly I was basing my attempt off of this link, which seems to imply that as long as the function definition has & in it, then I can just pass the object into the function by name. However I keep getting the following errors:

assembler.cpp: In function ‘int main()’: assembler.cpp:56:21: error: invalid initialization of non-const reference of type ‘Symbol_Table&’ from an rvalue of type ‘Symbol_Table (*)()’ get_input(Table_obj);

assembler.cpp:30:6: error: in passing argument 1 of ‘void get_input(Symbol_Table&)’ void get_input(Symbol_Table& Table_ptr)

As for my actual code, the relevant bits are below. The get_input function is just reading from a file and creating an object of a different type to work with. Through the course of the program running, the Symbol_Table object will have it's members changed so I need to pass it by reference without it being const, which was the only "solution" I could find.

void get_input(Symbol_Table& Table_ptr)
{
    do_stuff();
}

int main()
{
    Symbol_Table Table_obj();
    get_input(Table_obj);
}

I don't think the class definition of Symbol_Table is needed, but I can edit it in if needed.

Community
  • 1
  • 1
Dwayne H
  • 235
  • 2
  • 11

3 Answers3

1
Symbol_Table Table_obj();

That doesn't create a variable called Table_obj of type Symbol_Table, that's a declaration for a function called Table_obj which takes no arguments and returns a Symbol_Table.

Do one of these:

Symbol_Table Table_obj;
Symbol_Table Table_obj{}; //C++11
TartanLlama
  • 63,752
  • 13
  • 157
  • 193
1

You are declaring a function named Table_obj which will return Symbol_Table. This is not what you intend.

int main()
{
    Symbol_Table Table_obj; // Construct an object
    get_input(Table_obj);
}

This is like:

int get(); // Function

But you want:

int get; // Variable

Confusion reason:

int get(0); // int constructor takes one argument 
Ajay
  • 18,086
  • 12
  • 59
  • 105
1

The problem is that

Symbol_Table Table_obj();

does not make a Symbol_Table object named Table_obj but instead makes a function named Table_obj that takes nothing and returns a Symbol_Table. Change it to

Symbol_Table Table_obj;

and it will compile just fine.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • That's what I originally thought, but doing so gives me a huge error beginning with "myassembler: In function `_start': (.text+0x0): multiple definition of `_start'". – Dwayne H May 13 '16 at 11:38
  • @DwayneH: That's some other unrelated problem. You now need to go fix that problem. Previously you were only masking it. – Lightness Races in Orbit May 13 '16 at 11:39
  • Oh joy. Thanks then. Back to google I go. – Dwayne H May 13 '16 at 11:41
  • @DwayneH I agree with lRiO. your code (with some dummy stuff) [compiles](http://coliru.stacked-crooked.com/a/d5c60eee72445516)(ignore the warning). You must have another issue. – NathanOliver May 13 '16 at 11:41
  • Yeah fair enough. I'll accept the duplicate prompt since that did actually solve the problem. – Dwayne H May 13 '16 at 11:43