1

I am getting an error "undefined reference to fraction::from_string(char const*)" when I try to compile the program (shown in part below). I suspect that there is some issue with my conversion of a string to a C-style string, but I cannot seem to resolve it. I am able to call the from_string function outside the constructor where the error is occurring, but if I try to enable the code in the constructor, I get this error.

Here is my code. Yes, I believe there is a "ratio" or some similar object already in the standard. Not pertinent to the issue.

in file fraction.h:

…
class fraction {
public:
…
  fraction();
  fraction( int a );
  fraction( int a, int b, bool red );
  fraction( string inStr, bool red );
…
  static fraction from_string( const char* raw );
…
}

in file fraction_imp.h:

#include "fraction.h"
…
fraction from_string( const char* raw )
{
    // omitted for brevity. Parses the string to extract a numerator
    // and denominator for the fraction object
}
…
fraction::fraction( string inStr, bool red = false ) : auto_reduce(red)
{
    char* raw = const_cast<char*>( inStr.c_str() );
    fraction t = from_string( cRaw );
    numer = t.numer; denom = t.denom;
}

I get the following error from the compiler:

-------------- Build: D in fraction (compiler: MinGW-w64/GNU GCC 4.9.0 Compiler)---------------

x86_64-w64-mingw32-g++ -Wall -fexceptions -g -std=c++11 -c C:\cpp\fraction\main.cpp -o obj\D\main.o x86_64-w64-mingw32-g++ -o bin\D\rational.exe obj\D\main.o
obj\D\main.o: In function fraction::fraction(std::string, bool)': C:/cpp/fraction/fraction_imp.h:84: undefined reference tofraction::from_string(char const*)' collect2.exe: error: ld returned 1 exit status Process terminated with status 1 (0 minute(s), 2 second(s)) 1 error(s), 1 warning(s) (0 minute(s), 2 second(s))

I have tried a number of things to get the function call to work... at one point, the constructor looked like this:

fraction::fraction( string inStr, bool red = false ) : auto_reduce(red)
{
    char* raw = new char[inStr.size() + 1];
    copy(inStr.begin(), inStr.end(), raw);
    raw[inStr.size()] = '\0';
    char cRaw[50];
    strcpy( cRaw, raw );
    fraction t = from_string( cRaw );
    numer = t.numer; denom = t.denom;
}

but still no luck. Since the error shows undefined reference to fraction::from_string(char const*) I was not sure if the type I keep getting is a constant pointer to a character rather than a pointer to a constant character... ? Or is this because I am creating a temporary fraction object within a fraction constructor?

Arnestig
  • 2,285
  • 18
  • 30
oweinh
  • 15
  • 5

1 Answers1

4

It's got nothing to do with strings and chars.

fraction from_string( const char* raw )
{
    // ...
}

That's a new, previously undeclared, free function, not the static one from your class. You need:

fraction fraction::from_string( const char* raw )
{
    // ...
}
jrok
  • 54,456
  • 9
  • 109
  • 141
  • Yup, between my fiddling with the arguments to the from_string function and switching it back and forth from static to class method, I left it static in the declaration file and global, apparently, in the implementation file. Thanks for the second set of eyes... this has been driving me crazy for two days now. – oweinh Jun 30 '14 at 18:44