1

I have the following code and I am getting this error:

/usr/lib/gcc/x86_64-pc-linux-gnu/9.3.0/../../../../x86_64-pc-linux-gnu/bin/ld: /tmp/ccoDcMi1.o: in function `main':
activity17.cpp:(.text+0x19): undefined reference to `requestVariables(short, short, short)'

I understand the error, but I can't figure out what is wrong in my code.

#include <iostream>
#include <cmath>
#include <iomanip>

void requestVariables(short, short, short);
void longestPole(short, short, short);

int main(){
        short l, w, h;
        requestVariables(l, w, h);
        longestPole(l, w, h);
        return 0;
}

void requestVariables(short &l, short &w, short &h){
        std::cout << "Enter the length, width, and height of a room in meters:" << std::endl;
        std::cin >> l >> w >> h;
}

void longestPole(short l, short w, short h){
        float longest = sqrt(l*l+w*w+h*h);
        std::cout << "The longest pole that can fit in this room is: " << std::setprecision(2) << std::fixed << longest << std::endl;
}     
62626368616e
  • 321
  • 3
  • 9
  • Better to use pointers, not references, for the modified arguments. This way, people reading the caller will be able to tell that the arguments are modified because of the use of the reference operator `&` – lost_in_the_source Mar 30 '20 at 00:13
  • 2
    I disagree with the above opinion. – user4581301 Mar 30 '20 at 00:29
  • @EdwardKarak that is the address-of operator, not the reference operator. – M.M Mar 30 '20 at 01:39
  • 1
    @M.M "reference operator `&`" https://www.programiz.com/c-programming/c-operators – lost_in_the_source Mar 30 '20 at 01:45
  • @EdwardKarak "programiz.com" is not an authoritative source for C++ terminology – M.M Mar 30 '20 at 01:46
  • 1
    @M.M. Who cares, many sources call it "reference operator" while others call it "address operator." This is like the K&R vs OTBS debacle – lost_in_the_source Mar 30 '20 at 01:51
  • @EdwardKarak sorry for the late reply, just to clarify, the assignment asked for references specifically-- I actually learned pointers before references, we haven't gotten to pointers yet in my class lol – 62626368616e Mar 31 '20 at 02:49

1 Answers1

3

The forward declaration does not match the definition.

void requestVariables(short, short, short);

should be

void requestVariables(short &, short &, short &);
Notinlist
  • 16,144
  • 10
  • 57
  • 99
  • Ah I see, thank you so much! I must have skipped over the function prototyping for passing by reference by accident. – 62626368616e Mar 30 '20 at 00:12
  • @bbchan The clang flag `-Wmissing-prototypes` detects this, if you happen to be using that compiler (it will warn about the definition having no matching prototype) – M.M Mar 30 '20 at 01:52
  • @M.M I'll have to try it out and see how it is- I'm using GCC right now. thanks! – 62626368616e Mar 31 '20 at 02:51