Getting an undefined reference error

NOTE: THIS IS HOMEWORK. PLEASE POINT ME IN THE RIGHT DIRECTION - DO NOT JUST GIVE ME THE ANSWER. THANK YOU!

Here's my code: main.cpp (https://gist.github.com/rcpomasl/72bf819b95d46477b1dc), stddev.cpp (https://gist.github.com/rcpomasl/a8ac167b47e784e713ee), stddev.hpp (https://gist.github.com/rcpomasl/189df1517fc53bb17478). I'm including links rather than just posting my code here because I'm not sure where the error is, so I would need to post all of my code, which would probably take more space than is available.

Here's what the shell is giving me:
main.cpp: (.text + 0x190): undefined reference to 'stddev::computeAverage(std::string)'
main.cpp: (.text + 0x2a9): undefined reference to 'stddev::computeStdDev(std::string)'


I'm fairly new to C++. Do I need to do something additional to call functions that belong to another class? Have I made "Java" assumptions? Anything you can do to help will be appreciated. The lines that are calling those functions are lines 34 and 44.
Last edited on
The functions in stddev.cpp are global functions, not member functions. It'd be better to get rid of the class stddev, since it doesn't contain any data.
Could you give me a quick explanation on the difference between global and member functions? I'm inclined to keep stddev, if only because I will be reusing the code throughout the semester.
Why would you be unable to reuse the code if stddev wasn't there?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Foo
    //Member function
    void bar();
};

//Global function
void bar();

//Member function
void Foo::bar(){
}

//Global function
void bar(){
}
Last edited on
I would still be able to - I'm calling that my last resort if I can't get this figured out. I just want to have the code in it's own little package, rather than integrated into my main.

Based on advice from other people, I've added stddev:: to the functions in stddev.cpp, but this hasn't solved the problem.
I just want to have the code in it's own little package, rather than integrated into my main.
You don't need classes for that.
Well, I solved it. I needed to change the <> around the .hpp file include statement to " ".
> I just want to have the code in it's own little package
Use a namespace, perhaps?
http://www.cplusplus.com/doc/oldtutorial/namespaces/

Header stddev.hpp
1
2
3
4
5
6
7
#include <string>

namespace stddev // stat_utils would be a better name
{
    double computeAverage( std::string sequence ) ;
    // ...
}


Implementation stddev.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// #include <stddev.h>
#include "stddev.h" // use "" for your own headers
#include <sstream>

double stddev::computeAverage( std::string sequence )
{
    int sum = 0 ;
    std::istringstream numbers(sequence);
    int currentNum = 0;

    // this construct is canonical in C++
    while( numbers >> currentNum ) // for each number read from the stream
    {
        // validate range
        // ...
        // if valid, add to sum
    }
    return sum ;
}

// ... 


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "stddev.h" 

// ...

int main()
{
    std::string current_line ;

    // ...

    auto avg = stddev::computeAverage(current_line) ;

    // ...
}

// ... 
Topic archived. No new replies allowed.