Note that there are some explanatory texts on larger screens.

plurals
  1. POC++: Unresolved externals driving me nuts
    text
    copied!<p>I'm taking a class on C++ and this is driving me berserk. The funnest part is that no guide seems to tell me exactly what to do and no question seems to address the problem here. It's probably due in part to the issue of multiple C++ compilers. I'm using Visual Studio 2010 and I'm just trying to build a class with operator overloading. I have my header, class, main, etc:</p> <pre><code>//File Rational.h #ifndef RATIONAL_H #define RATIONAL_H class Rational { private: int numerator; int denominator; public: Rational(int num = 0, int den = 1); Rational operator+(const Rational &amp;); Rational operator=(const Rational &amp;); Rational operator+=(const Rational &amp;); int getNum(); int getDenom(); }; #endif </code></pre> <p>And the class:</p> <pre><code>#include "stdafx.h" #include &lt;iostream&gt; using namespace std; class Rational { private: int numerator; int denominator; public: Rational(int num = 0, int den = 1) { numerator = num; denominator = den; } Rational operator+(const Rational &amp;r) { return Rational(((numerator * r.denominator) + (r.numerator * denominator)), (denominator * r.denominator)); } Rational operator=(const Rational &amp;r) { denominator = r.denominator; numerator = r.numerator; } Rational operator+=(const Rational &amp;r) { return Rational(((numerator * r.denominator) + (r.numerator * denominator)), (denominator * r.denominator)); } </code></pre> <p>The getter functions are what you'd expect and don't error out, but the constructor and overloaded methods give me the unresolved external. I can't even remember how much stuff I've tried in the methods and the header. I've seen a dozen or so guides and examples, and none of them have worked. I did get this to compile once with the + operand as a friend function, but it needs to be a member function for the assignment, and moving it to a member function somehow managed to break all of the above. I'm going nuts here. It won't resolve those methods no matter what I do!</p> <p>The main method invokes all this in the standard fashion and includes the Rational.h file. All files are in the same directory.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload