473,416 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

An obligatory "Undefined Reference" problem ( with a twist! )

36
Hello all! ( again )

Once more, I have a problem that seems unsolvable by me.

I'm getting the, seemingly common, "undefined reference" linking error.

I've tried quite a few things, but nothing is working. I'll list what I've tried at the
end of my post.

Until then, here's the error message, and the code ( Sorry...( length ) ) :

Error Message:
[Linker error] undefined reference to `Array<student*>::Array(int)'


main.cpp
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include "arrayClass.hpp"
  4. #include "schoolClass.hpp"
  5.  
  6.  
  7. int main()
  8. {
  9. <snipped>
  10.  
  11.   return 0;
  12. }
  13.  

arrayClass.hpp
Expand|Select|Wrap|Line Numbers
  1. #ifndef ARRAY_CLASS_H
  2. #define ARRAY_CLASS_H
  3.  
  4. <snipped>
  5.  
  6. #endif
  7.  

arrayClassDef.cpp
Expand|Select|Wrap|Line Numbers
  1. #include "arrayClass.hpp"
  2.  
  3. <snipped>
  4.  

schoolClass.hpp
Expand|Select|Wrap|Line Numbers
  1. #ifndef SCHOOL_CLASS_H
  2. #define SCHOOL_CLASS_H
  3.  
  4. #include <string>
  5. #include "arrayClass.hpp"
  6.  
  7. <snipped>
  8.  
  9. #endif
  10.  

Whew! Ok, moving on...

I've tried including the two headers in all four files ( in different combinations )
I've tried placing the arrayClass' definitions in both, arrayClass.hpp, & main.cpp

I added the line: #include "arrayClassDef.cpp" to 'main', and it worked, but I read
somewhere, that you shouldn't do that ( or have to do that ).

The kicker: If I put all of this into one file, it compiles without a hitch.
Runs fine too.

I'm missing something, I just don't know what it is yet.

Help me, oh great scripters! ( LOL ) :)


Well, there ya go. I think I've posted everything...

Oh, I'm using Win-doze XP, and Dev-C++ 4.9.9.2

I tried this in MSVC++ also, and the error said something like: Could not open arrayClassDef.cpp, no such file or directory.

It said that twice. Hmm...

I'm off, lates!

-Soneji™
Jul 24 '07 #1
8 1921
Banfa
9,065 Expert Mod 8TB
I have cut out your code because this looks like it may be a coursework assignment and we have a policy against posting the full code for coursework assignments.

Please read our posting guidelines.

Anyway I do not think the code is central to your problem, particularly since it works when you put it all in 1 file.



The problem you are experiencing is common for less experienced programmers. There are 2 (basic) stages to creating an executable
  1. Compile all the source files to object files
  2. Link all the object files with relevent libraries into an executable

Your error comes from stage 2 and the most likely cause is that you are not including object created by compiling arrayClassDef.cpp into the link. You may not even be compiling arrayClassDef.cpp.

What command line or makefile are you using to create your project? Is it something like

cl main.cpp

in which case try

cl main.cpp arrayClassDef.cpp
Jul 24 '07 #2
Soneji
36
HAHAHAHA!!!!

I appreciate the help, I really do, but I'm not in school.

This isn't an assignment, it's from a C++ tutorial book that I am using to learn the language at home, by myself ( It's not identical of course, since the original has typos that render it useless ).

( Oh, and I have read the posting guidelines. That's why I searched extensively before I posted the question.
I've seen it asked before, but none of the answers had fixed my problem. )

Since I started, I've been making a project in Dev-C++, and adding all necessary source files, and headers to that project, then compiling it.

This has worked fine until this project.

Now, if I compile the source code into object files, that will lead into more questions:
1) Will I need to link the header files to them when I link the object files to each other.
2) If I compile both source code files, then how do I link them together without making a new source file to compile? ( Does that last one make sense? )

I'm off to try again!

Lates,
-Sonej™
Jul 24 '07 #3
Banfa
9,065 Expert Mod 8TB
OK the tool chain (compiler, linker, etc) works something like this

The source (cpp, cxx, c) files and header (h, hpp, hxx) files are inputs for the compiler. The compiler compiles them to produce object files. The source files are direct inputs to the compiler, the headers are not, they are indirectly referenced through the #include statements in the source files.

These object files plus any third party object files, plus the standard libraries plus any third party libraries sometimes plus other files defining the platform depending on what the platform is are inputs to the linker. The linker links them to produce an executable image.

Sometimes there is a third stage to convert the executable image to the image to be programmed into the flash memory of the platform.

Expand|Select|Wrap|Line Numbers
  1. -------------      -------------
  2. |           |      |           |
  3. | File1.cpp |      | File2.cpp |
  4. |           |      |           |
  5. -------------      -------------
  6.       |                  |
  7.       |                  |
  8.       V                  V
  9.  /---------\        /---------\
  10.  |         |        |         |
  11.  |Compiler |        |Compiler |
  12.  |         |        |         |
  13.  \---------/        \---------/
  14.       |                  |
  15.       |                  |
  16.       V                  V
  17. -------------      -------------      -------------
  18. |           |      |           |      | Standard  |
  19. | File1.obj |      | File2.obj |      | Libraries |
  20. |           |      |           |      |           |
  21. -------------      -------------      -------------
  22.       |                  |                  |
  23.       |                  |                  |
  24.       |________________  |  ________________|
  25.                        | | |
  26.                        | | |
  27.                        V V V
  28.                     /---------\
  29.                     |         |
  30.                     | Linker  |
  31.                     |         |
  32.                     \---------/
  33.                          |
  34.                          |
  35.                          V
  36.                    -------------
  37.                    |           |
  38.                    |Executable |
  39.                    |  Image    |
  40.                    -------------
  41.  
However I notice you are using an IDE (Dev-C++ or MSVC) these should handle all this for you, you just add you source and header files to the project and the IDE should deal with the compiling and linking.
Jul 24 '07 #4
Soneji
36
However I notice you are using an IDE (Dev-C++ or MSVC) these should handle all this for you, you just add you source and header files to the project and the IDE should deal with the compiling and linking.
Yes, I'm using Dev-C++ ( I also tried it in MSVC++ ).

I created a project, added both source files, and both headers.

However, when I compile, I get the error mentioned above.

That's what has me frazzled. I've done other programs this way before, and I've never had this problem.

If I were trying to compile/link via the command-line, then I could understand if it didn't work...

But, the IDE should do all the work for me, and it's not.

After some searching, I found something closely related to my problem at parashiftcom ( C++ FAQ Lite ).
This is the page.

Of course, adding the line: template class Array<students *> to the class definition source doesn't work.
( for the record, I just tried to compile the example on that site, and it doesn't
work either. Even after the change he suggests. )


I don't know if it will help, but here's the makefile that was created in my latest failed attempt at compiling this project:

Expand|Select|Wrap|Line Numbers
  1. # Project: Project1
  2. # Makefile created by Dev-C++ 4.9.9.2
  3.  
  4. CPP  = g++.exe
  5. CC   = gcc.exe
  6. WINDRES = windres.exe
  7. RES  = 
  8. OBJ  = main.o arrayClassDef.o $(RES)
  9. LINKOBJ  = main.o arrayClassDef.o $(RES)
  10. LIBS =  -L"lib" -L"C:/-=C++=-/-=CODE=-/MyLibraries"  
  11. INCS =  -I"include" 
  12. CXXINCS =  -I"lib/gcc/mingw32/3.4.2/include" 
  13.     -I"include/c++/3.4.2/backward"  -I"include/c++/3.4.2/mingw32"
  14.     -I"include/c++/3.4.2"  -I"include"  -I"C:/-=C++=-/-=CODE=-/MyHeaders" 
  15. BIN  = diffObj.exe
  16. CXXFLAGS = $(CXXINCS)  
  17. CFLAGS = $(INCS)  
  18. RM = rm -f
  19.  
  20. .PHONY: all all-before all-after clean clean-custom
  21.  
  22. all: all-before diffObj.exe all-after
  23.  
  24.  
  25. clean: clean-custom
  26.     ${RM} $(OBJ) $(BIN)
  27.  
  28. $(BIN): $(OBJ)
  29.     $(CPP) $(LINKOBJ) -o "diffObj.exe" $(LIBS)
  30.  
  31. main.o: main.cpp
  32.     $(CPP) -c main.cpp -o main.o $(CXXFLAGS)
  33.  
  34. arrayClassDef.o: arrayClassDef.cpp
  35.     $(CPP) -c arrayClassDef.cpp -o arrayClassDef.o $(CXXFLAGS)
  36.  
I'm getting frustrated to the point of saying, "To hell with this particular program".

It compiled as a single source file. I saw it's output.

The only reason I'm so focused on getting this to work correctly, is just in case I
come across this problem again.


Edit: I just made both source files into object files, then I tried to link them in the command-line with g++, using this line:
g++ -o mine main.o arrayClassDef.o

It gave me the 'undefined reference' message again.

*sigh*

I'm off to try again...

-Soneji™

"George is getting upset!"
Jul 24 '07 #5
Banfa
9,065 Expert Mod 8TB
Damn, right sorry, I've lead you right up the garden path. And now I wish I hadn't deleted quite so much of the contents of arrayClassDef.cpp.

You have a template class declared in arrayClass.hpp and defined in arrayClassDef.cpp.

For a normal class a 2 file (1 cpp and 1 h/hpp) is the normal way to go, however this is not how it works for templates. The definition is arrayClassDef.cpp is not a definition but a pattern describing how to define the class when it is required.

As such the pattern needs to be visible when the class is used so that the specialisation can be created.


I normally take 2 approaches to this either
  1. Put everything in 1 file, I normally use a different extension (hxx) for a header containing a template.
  2. Use 2 files, 1 for the declaration and 1 for the pattern (unspecialised definition) following the normal pattern of files for classes. I give the declaration file the normal header extension (h/hpp) and the file containing the unspecialised function definitions an alternate name (cxx). I then include the cxx file into the h file just below the class declaration (or just include them both where needed).

Anyway the point is that I use alternate extensions to indicate the contents of the file and that the template definition must be visible to the compiler when the code is trying to use the template and it is considered bad form to include cpp files.

Basically you don't compile template separately because they are dependent upon how they are used.



I am going to get another mod to look at this and make sure I have got it right this time.
Jul 25 '07 #6
Soneji
36
Heh... I'm glad I decided to post the makefile!

Make sure I understand this...
If I wasn't using the template, then the way I was doing it would be the preferred method.

However, with the template, I need to either: add the "definitions" to the header with the class declaration
( and I can alter the extension so I can tell it apart from my other headers )...

Or, do it like I was doing it, except add this line to the bottom of the header: #include arrayClassDef.cxx
( where xx are letters of my choosing. Again, to tell it apart. )

Is this the gist of it? :)

I had read on another site, the same thing you said in your last post:
Including a source file is frowned upon, so I guess that really only leaves one option when
dealing with templates, eh?

BRB, going to check to see if this works ( I know it will... )
I'LL BE!!! You fixed my problem!
I put the contents of arrayClassDef into the arrayClass header, and it compiled without so much as a hiccup.

Thank you so very much! I've learned something new!
Well, two things actually...

1) When using templated classes, put the "pattern" ;) in the header with the declaration.
2) Make sure that I put: Not an assignment at the top of my posts from now on. :P

Thanks again!

Maybe I'll see you in my next post.
( Hexadecimal has been razzing me from the get-go )

Lates,
-Soneji™


P.S. I forgot to ask this earlier... If you have more than one class, what's the proper way to split the declarations, and definitions among files?

I mean, say you have 2 classes, should both class declarations be in one header, and both definitions be in one source file, or should each get their own file
( 2 headers, 2 source files )?
Jul 25 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
Banfa asked me to look at this thread.

Without rehashing what you have already done, I wanted to be sure you understand that:

1) a template is not code.
2) a template is used by the compiler to create classes and functions by using a real type for the template type. The template is just the pattern. A template is a direct replacement for a C macro.
3) these created classes and functions are called instances of the template. These are the ones that are actually used in the program.
4) because the template is the pattern used by the compiler to create classes and functions, the templates have to included in every source file. Usually, this is done by including the templates in a header file.
5) any templates must be at the global level.

Because of the above:
1) there is no such thing as a template function prototype.
2) not all templates work with all types.
3) because of (2) you may need to write your own functions that use the type the template can't use.
4) becuse of (3) you have to tell the compiler to not use the template for the type where you have written a function. This is where you use the explicit specialization.
Jul 25 '07 #8
Soneji
36
OOPS!

Sorry, I've been busy, and couldn't get back here to say: Thanks Weakness!

<Soneji>
Jul 29 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Steven T. Hatton | last post by:
Scroll to the bottom and read the last part first. I've been trying very diligently to 'modularize' the code from TC++PL3E found here: http://www.research.att.com/~bs/matrix.c I keep getting...
13
by: Don Vaillancourt | last post by:
What's going on with Javascript. At the beginning there was the "undefined" value which represented an object which really didn't exist then came the null keyword. But yesterday I stumbled...
4
by: Flip | last post by:
I'm seeing one of my websites giving me an error message on the page after it loads up as "WXBUnit Undefined" What does that mean? The formatting of the site is horrible! I'm not sure what is...
49
by: matty | last post by:
Hi, I recently got very confused (well that's my life) about the "undefined" value. I looked in the FAQ and didn't see anything about it. On...
9
by: Klaus Johannes Rusch | last post by:
IE7 returns "unknown" instead of "undefined" when querying the type of an unknown property of an object, for example document.write(typeof window.missingproperty); Has "unknown" been defined...
5
by: Jason | last post by:
Hello, I am trying to dynamically create a table, then set its <td>'s onclick: var table = document.createElement("table"); var row = table.insertRow(-1); var td = row.insertCell(-1);...
3
by: s.z.s | last post by:
Hi! I hope the solution to that is not too stupid... I've got three files: <snip test_main.cc> #include"test.hh" int main(void) { A<inta1; a1.saywhat();
15
by: bill | last post by:
I am trying to write clean code but keep having trouble deciding when to quote an array index and when not to. sometimes when I quote an array index inside of double quotes I get an error about...
8
Motoma
by: Motoma | last post by:
Good evening everyone. I am starting to re-explore C++, and I wanted to build a singleton class. Unfortunately, when I set things up as I do in PHP, it doesn't work out for me. I hope that the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.