compiling in dev-cpp

Started by
7 comments, last by kooooo 18 years, 4 months ago
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>
kooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
Advertisement
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++
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++
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.
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++
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.
kooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
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 :)
kooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
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;
thanks a lot, it finally works!
kooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo

This topic is closed to new replies.

Advertisement