thank you both so far! I'm a newbie... basically got thrust into the middle of C++ from a Java background, albeit from a bit ago and only a newbie at that. Frustrating as I understand the basic concepts, but the syntax is killing me. Im also learning that using Visual Studio as an IDE may not be the best thing.
@Enoizat: Thank you for the sanity checking with my boolean. I looked back at it and it made no damn sense in any manner. I have an incredibly bad habit of getting frustrated and going back and trying to edit my code to "make it work".. obviously this NEVER WORKS... I liken it to a nervous tic of mine and something that I'm working on.
@ne555: Thank you as well. the first comparison statement doesn't make a lick of sense. looking at the main. All I'm trying to do is use the == (overloaded) to output one of the " x1==x2" etc.. options in the "cout << (y1 == y2 ? "y1 == y2" : "y1 != y2")" type of lines dependant upon whether true or false. Given this, what I had does not make any sense whatsoever.
I also agree that just because someone may copy my code, doesn't mean that I may learn less. However it does make it a pain in the butt to prove that I just didnt grab code from all places of the internet. That being said.. Code is posted below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#include <iostream>
using namespace std;
template <class T>
class strange
{
public:
strange(T a, T b);
friend ostream& operator<< <> (ostream& osObject, const strange<T>& sObject);
friend bool operator==(const strange<T>& one, const strange<T>& two);
private:
T a;
T b;
};
template <class T>
strange<T>::strange(T first, T second) {
a = first;
b = second;
}
template <class T>
ostream& operator<< (ostream& osObject, const strange<T>& sObject) {
osObject << sObject.a << ", " << sObject.b;
return osObject;
}
template <class T>
bool operator==(const strange<T>& one, const strange<T>& two)
{
return (one.a == two.a && one.b == two.b);
}
int main()
{
strange<int> x1(13, 12), x2(130, 2);
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
cout << "y1 = " << y1 << endl;
cout << "y2 = " << y2 << endl;
return 0;
}
|
everything actually compiles without error as long as I do not uncomment the two lines in the main. As soon as I do I get an "LNK2019: unresolved external symbol "bool __cdecl operator==(class strange<int> const &,class strange<int> const &)" (??8@YA_NABV?$strange@H@@0@Z) referenced in function _main" error. Is this something that is perhaps resident to VS, or is it an actual issue? I've realized that VS tends to throw weird wonky crap out due to the compiler itself v. actual errors. Due to this, sometimes I question if its my code (most likely) or the compiler (microsoft product...so still likely).
I'm feeling that the root of the error lies in referencing templates outside of the class and vice versa... perhaps the "friend" for my boolean isnt so friendly?