-1

When I pass a vector's address using the syntax below:

void myfunction(std::vector<double>*);
int main()
{
    std::vector<double> t;
    myfunction(&t);
    return 0;
}
void myfunction(std::vector<double> &v)
{
    cout << "The function ran" <<endl;
}

I receive this error and I don't know why.

pal-nat184-134-146:p25 pdevieti$ g++-4.9 test.cpp 
Undefined symbols for architecture x86_64:
  "myfunction(std::vector<double, std::allocator<double> >*)", referenced from:
      _main in ccVmpacj.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
Peter
  • 367
  • 1
  • 4
  • 12
  • Your `myfunction` declaration doesn't match the definition. – juanchopanza Jan 27 '15 at 21:42
  • In the declaration of `myfunction` the argument is a pointer to a vector of doubles, where as in the definition, you take a reference to the same kind of vector – notadam Jan 27 '15 at 21:43
  • 1
    You reinstalled the OS and compiler because you were getting a linker error??! – Praetorian Jan 27 '15 at 21:48
  • Your code is calling a function that doesn't exist. The compiler doesn't know that the function doesn't really exist. That job is left to the linker, and nowhere did the linker find the function that takes the pointer to the vector. So you did all of this uninstalling/reinstalling game for no reason whatsoever. – PaulMcKenzie Jan 27 '15 at 21:52

1 Answers1

4

Chage this:

void myfunction(std::vector<double>*);

to this:

void myfunction(std::vector<double>&);

The error is that signatures in the declaration and definition of "myfunction" are different. You declare myfunction as a function that receives a pointer to vector, but define it as a function that receives a reference. References and pointers are different things from the language's perspective.

Refer to this thread fro some very detailed explanations.

Community
  • 1
  • 1
nicebyte
  • 1,498
  • 11
  • 21
  • I would go the other way though, that is to use references – notadam Jan 27 '15 at 21:44
  • 1
    It depends. References don't make sense in cases when it is possible for the corresponding object to be absent. In that case it makes sense to use a pointer and use `nullptr` to indicate absence. So the OP should judge which is best for their use case. – nicebyte Jan 27 '15 at 21:46
  • Awesome, thank you, that solved it. If I am simply passing an array to store values of doubles, is it better to pass through reference or pointer? – Peter Jan 27 '15 at 21:50
  • The idiomatic approach in C++ is to use references until you can't. In this case there is no indication the argument is optional. Using references should be the recommendation here with a site not about and links for choosing to use pointers for optional parameters. – Captain Obvlious Jan 27 '15 at 21:51
  • Seems like it's better to use references in your case. – nicebyte Jan 27 '15 at 21:53
  • 1
    Great. I switched them to reference in my original program and now I'm off to new errors. Thanks for the help. – Peter Jan 27 '15 at 22:02