Sign in to follow this  
kooooo

compiling in dev-cpp

Recommended Posts

i'm trying to use Dev-C++ to compile a quick little program that uses templates. Unfortunately, it isn't working. I am just learning templates, but I am pretty sure that Dev-C++ had me stick <T> in too many places... also, on the side-panel on the left side, when I click the class tab, it shows me two member functions and main(). It can compile, but refuses to link and I get these messages: [Linker error] undefined reference to `csmart<int>::csmart(int)' There are more, all exactly alike, or with a different member function. Here is the code for my program: <code> // file: csmart.h template <typename T> class csmart { private: //member variables T * m_value; int m_valid; public: //member functions csmart() { m_value = 0; m_valid = 0; } csmart(T val); ~csmart(); void SetValid(int valid); int Valid(); T Value(); csmart & operator=(const csmart & rhs); csmart & operator=(const T & rhs); }; // file: csmart.cpp #include "csmart.h" template <typename T> csmart<T>::csmart(T val) { //set m_value and NULL m_valid m_value = val; m_valid = 0; } template <typename T> csmart<T>::~csmart() { delete m_value; } template <typename T> void csmart<T>::SetValid(int valid) { //set m_valid to valid m_valid = valid; } template <typename T> int csmart<T>::Valid() { return m_valid; } template <typename T> T csmart<T>::Value() { return m_value; } template <typename T> csmart<T> & csmart<T>::operator=(const csmart<T> & rhs) { //set variables equal if (m_value == rhs.Value()) return *this; m_value = rhs.Value(); m_valid = rhs.Valid(); return *this; } template <typename T> csmart<T> & csmart<T>::operator=(const T &rhs) { //set to Type T - sort of dangerous if (m_value == rhs) return *this; m_value = rhs; return *this; } // file: main.cpp #include <iostream> #include "csmart.h" using namespace std; int main() { csmart<int> s_int(12); csmart<int> s_int_b(19); cout << "\nValue of s_int: " << s_int.Value(); cout << "\nValue of s_int_b: " << s_int_b.Value(); cout << endl; cout << "Swapping values..."; int temp; temp = s_int.Value(); s_int = s_int_b; s_int_b = temp; cout << endl << endl; cout << "Done.\n"; cout << "\nValue of s_int: " << s_int.Value(); cout << "\nValue of s_int_b: " << s_int_b.Value(); cout << endl << endl; cout << "enter any value to quit: "; char quit; cin >> quit; return 0; } </code>

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I don't know about this, but try .... Other than that, I haven't the slightest clue to why it's giving undefined references to '...'.

csmart.cpp IS in your project, correct?

C++

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Anonymous Poster
I don't know about this, but try .... Other than that, I haven't the slightest clue to why it's giving undefined references to '...'.

csmart.cpp IS in your project, correct?

C++


'....' ???

It was supposed to be:template

I think it messed up the '' with html.

C++

Share this post


Link to post
Share on other sites
Unless your compiler supports the export keyword, and gcc doesn't, then you can't put the definition of a template in a separate source file without explicit instantiation for specific types. Without explicit instantiation, the complete definition of the template needs to be available at point of instantiation, which means, in effect, that the definition needs to go into the header. (Or an inline file of some sort, etc.)

For more details see these articles: "Export" Restrictions, Part 1 and "Export" Restrictions, Part 2.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Anonymous Poster
Quote:
Original post by Anonymous Poster
I don't know about this, but try .... Other than that, I haven't the slightest clue to why it's giving undefined references to '...'.

csmart.cpp IS in your project, correct?

C++


'....' ???

It was supposed to be:template

I think it messed up the '' with html.

C++


S**t. Look:

// It messed up '<' and '>' with html.

// It could be:
template<class T>
class ... { ... };



I hope it works...

C++

Share this post


Link to post
Share on other sites
thanks. I put the code from csmart.cpp into csmart.h, then removed csmart.cpp from my project. It had errors, but no more of the weird compiler linking stuff.

Share this post


Link to post
Share on other sites
lol... i can't figure out the rest of these errors... here they are:

C:\Dev-Cpp\projects\smartdata\csmart.h In member function `csmart<T>& csmart<T>::operator=(const csmart<T>&) [with T = int]':
18 C:\Dev-Cpp\projects\smartdata\main.cpp instantiated from here

75 C:\Dev-Cpp\projects\smartdata\csmart.h passing `const csmart<int>' as `this' argument of `T csmart<T>::Value() [with T = int]' discards qualifiers

76 C:\Dev-Cpp\projects\smartdata\csmart.h passing `const csmart<int>' as `this' argument of `int csmart<T>::Valid() [with T = int]' discards qualifiers

i would appreciate somebody helping me finish this off :)

Share this post


Link to post
Share on other sites
In order to call those functions on the rhs object in your assignment operator you need to make the member functions const. ex:

int Valid() const;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this