delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/11/29/10:06:56

From: "Per Pettersson" <ostybosty AT hotmail DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: RHIDE C++ project link failure
Date: Wed, 29 Nov 2000 15:26:59 +0100
Organization: Pennsylvania College of Technology
Lines: 82
Message-ID: <9033kq$4u10@NWNEWS.PCT.EDU>
NNTP-Posting-Host: 193.11.234.144
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2615.200
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Howdy fellas,

I am an intermediate programmer, trying to make a general template list
class in C++. The class itself works fine, but when I try to run a test
program for it, it compiles but it won't link.

I have three files: list.h with class declaration, list.cpp with class
definition and main.cpp with test routines, all in the same project. I
#include "list.h" in both .cpp sources, but no matter what I do, compilation
of main.cpp fails, with error messages like I hadn't #included the header at
all ("undefined reference" to the class and all its methods). I tried
changing the linking order, but no difference.

The funny thing is that, if I put the main() function in list.cpp (where it
most definitely doesn't belong!) the program will link just fine. I can't
seem to figure out why this is?

Could you help me out (it's not my homework so no DYODH's please)?
Below is shown the structure of the three files (this is from memory, not
actual code):

(list.h)
---------
#ifndef _OYP_LIST_H_
#define _OYP_LIST_H_

namespace OYP
{
  template <class T>
  class List
  {
    // tons of declarations...
  };
}

#endif

(list.cpp)
----------
#include "list.h"

using OYP::List;

template <class T>
List<T>::List()
{
  // code
}

// and all the other methods declared in list.h are defined in this file

(main.cpp)
----------
#include <iostream>
#include "list.h"

using OYP::List;

int main()
{
  List<int> lizzy; // not even anything this simple will do
  // Says "undefined reference to OYP::List<int>::List(void)" - WHY?
  // I HAVE defined the methods, haven't I?
  lizzy.add(3).add(5); // and of course this won't work;
  cout << lizzy[0] << ", " << lizzy[1]; // neither will this
  return 0;
}

Thank you for reading this!

---
Per Pettersson
Computer Science Student
University of Gothenburg, Sweden








- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019