LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-08-2005, 04:04 PM   #1
byteframe
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 62

Rep: Reputation: 15
C++ Inheritance with Multiple Files, #include crap Hard Help please


I have one class called TextFile.
I have two other classes that I want to extend TextFile: Correlation, and Rotation.
I'm getting linking erros whenever I try to define the constructors for both these objects. I don't know what's going on, but I also don't really know the best way to organized 3 headers files (one for each class) 3 cpp Source files (one for each class) and of course a source file for the main program. I fiqure if I can nail down organizing the files, espeically how to #include correctly, and use inclusion guards correctly, I fiqure I can attribute the problems to my code, and not how they call link together.

I've tried tons of stuff, but I fiqure I'll post what I have for code now, (trimmed down to save space on LQ of course...)

TextFile.h

Code:
#ifndef TEXTFILE_H
 #define TEXTFILE_H

#include <string>

class TextFile {
    protected:
        // members
        
    public:
        TextFile();
        TextFile(std::string);
        // other methods
};

#endif
TextFile.cpp

Code:
#include "TextFile.h"
#include <fstream>

// method definitions
Rotation.h

Code:
// Rotation Class Interface
#ifndef ROTATION_H
 #define ROTATION_H

#include "TextFile.h"

class Rotation : public TextFile {
    private:
        // members    
    public:
        Rotation();
        // other methods
};

#endif
Rotation.cpp

Code:
// Rotation Class Definition

#include "Rotation.h"

Rotation::Rotation() {
    ;
}
Correllation.h

Code:
// Correlation Class Interface
#ifndef CORRELATION_H
 #define CORRELATION_H

#include "TextFile.h"

class Correlation : public TextFile {
    private:
        // members       
    public:
        Correlation();
        // other methods
};

#endif
Correllation.cpp

Code:
// Correlation Class Definition

#include "Correlation.h"

Correlation::Correlation() {
    ;
}

void Correlation::Correlate() {
    ;
}
--------------------------------------------------
I havent really defined anything in Rotation.cpp or Correllation.cpp yet, as I fiqure after I learn how to work with inheritance in C++ and multiple files and I can get back to CODING.

I'm currently in the lab at school here working on really really mismanaged windows boxes (I run slackware at home) and I havent tested out my code at home. Yet thisis with BloodShed DevC++, which uses minigw, and my teacher of course has a windows box at home, so I wouldnt be suprised if I get them same errors, and it's not the IDE's fault.

Please assist, Thank you very much. Screw ATI.
 
Old 12-08-2005, 05:42 PM   #2
byteframe
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 62

Original Poster
Rep: Reputation: 15
This is what I get for an error message on my box.
Code:
/tmp/ccGbVUHN.o(.text+0xd): In function `Correlation::Correlation[not-in-charge]()':
: undefined reference to `TextFile::TextFile[not-in-charge]()'
/tmp/ccGbVUHN.o(.text+0x23): In function `Correlation::Correlation[in-charge]()':
: undefined reference to `TextFile::TextFile[not-in-charge]()'
/tmp/cczVb7IX.o(.text+0xd): In function `Rotation::Rotation[not-in-charge]()':
: undefined reference to `TextFile::TextFile[not-in-charge]()'
/tmp/cczVb7IX.o(.text+0x23): In function `Rotation::Rotation[in-charge]()':
: undefined reference to `TextFile::TextFile[not-in-charge]()'
collect2: ld returned 1 exit status
byteframe@euclid:~/funFunTextFiles/src$
byteframe@euclid:~/funFunTextFiles/src$
 
Old 12-08-2005, 05:42 PM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
are you #define indented?
if you want to indent them do it like
Code:
#    define _WHAT_EVER_H_
do the constructors have bodies?


Code:
void Correlation::Correlate() {
    ;
}
you can not specify the return type of a constructor.
<edit> oops i see the above func is not a constructor but a member func which is not in the class declaration.
</edit>
change the above code to
Code:
Correlation::Correlate() : TextFile()
{
    ;
}
this then calls and intailises the base class before entering the constructor called, but make sure you have a body for the constructor or at the very least have something like.
Code:
class TextFile 
{
    protected:
        // members
        
    public:
        TextFile(){;}
        TextFile(std::string){;}
        // other methods
};

Last edited by dmail; 12-08-2005 at 05:47 PM.
 
Old 12-08-2005, 06:21 PM   #4
byteframe
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 62

Original Poster
Rep: Reputation: 15
What does changing the whitepace do for the #define ?
I added : TextFile() before the first { on both constructors and I still get errors...

Code:
/tmp/ccbZRWPh.o(.text+0xd): In function `Correlation::Correlation[not-in-charge]()':
: undefined reference to `TextFile::TextFile[not-in-charge]()'
/tmp/ccbZRWPh.o(.text+0x2d): In function `Correlation::Correlation[in-charge]()':
: undefined reference to `TextFile::TextFile[not-in-charge]()'
/tmp/ccSx1ELC.o(.text+0xd): In function `Rotation::Rotation[not-in-charge]()':
: undefined reference to `TextFile::TextFile[not-in-charge]()'
/tmp/ccSx1ELC.o(.text+0x2d): In function `Rotation::Rotation[in-charge]()':
: undefined reference to `TextFile::TextFile[not-in-charge]()'
collect2: ld returned 1 exit status
byteframe@euclid:~/funFunTextFiles/src$
 
Old 12-08-2005, 06:31 PM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by byteframe
What does changing the whitepace do for the #define ?
ansi requires that there are no white space before '#'

do the constructors have bodies?

Last edited by dmail; 12-08-2005 at 06:38 PM.
 
Old 12-08-2005, 06:40 PM   #6
byteframe
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 62

Original Poster
Rep: Reputation: 15
I added the indents and both have bodies....
Code:
TextFile::TextFile(std::string in_lastFile) {    
    buffer = "";
    lastFile = in_lastFile;
    wordCnt = 0;
    charCnt = 0;
}
Code:
Correlation::Correlation() : TextFile() {
    score = -1;
}
Code:
Rotation::Rotation() : TextFile() {
    key = 0;
}
Same errors...

Code:
/tmp/ccIixfBH.o(.text+0xd): In function `Correlation::Correlation[not-in-charge]()':
: undefined reference to `TextFile::TextFile[not-in-charge]()'
/tmp/ccIixfBH.o(.text+0x2d): In function `Correlation::Correlation[in-charge]()':
: undefined reference to `TextFile::TextFile[not-in-charge]()'
/tmp/ccbkZ9pL.o(.text+0xd): In function `Rotation::Rotation[not-in-charge]()':
: undefined reference to `TextFile::TextFile[not-in-charge]()'
/tmp/ccbkZ9pL.o(.text+0x2d): In function `Rotation::Rotation[in-charge]()':
: undefined reference to `TextFile::TextFile[not-in-charge]()'
collect2: ld returned 1 exit status
byteframe@euclid:~/funFunTextFiles/src$

Last edited by byteframe; 12-08-2005 at 06:41 PM.
 
Old 12-08-2005, 06:51 PM   #7
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
what about the default constructor for textfile, does this have a body?

if so then i think i will probably need to see your full code as i dont understand the errors "not in charge".

Last edited by dmail; 12-08-2005 at 06:52 PM.
 
Old 12-08-2005, 06:56 PM   #8
byteframe
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 62

Original Poster
Rep: Reputation: 15
Aha your right! I didnt define my DEFAULT constructor for TextFile! So do use my other constructor would I use : TextFile(std::string) instead of : TextFile(), and it would run TextFiles's constructor, using the string parameter for that constructor before it then went on to my derived objects constructor?
 
Old 12-08-2005, 06:58 PM   #9
byteframe
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 62

Original Poster
Rep: Reputation: 15
Also, what are your opinions on the following C++ coding conventions.
1. should I use _HEADER_H_ or HEADER_H?
2 should I ALWAYS use braces {} for control structures that only execute one statement?
 
Old 12-08-2005, 07:00 PM   #10
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
thats true yes, but you should still give it a body if you leave it there. by using the constructor which takes a string any derived class constructor needs to supply the string argument like so:
Code:
Correlation::Correlation(std::string something) : TextFile(something) 
{
    score = -1;
}
Quote:
Also, what are your opinions on the following C++ coding conventions.
1. should I use _HEADER_H_ or HEADER_H?
2 should I ALWAYS use braces {} for control structures that only execute one statement?
1)it is frowned upon using _HEADER_H_ as windows uses this method, but i use it.
2)I always used braces,even if its on the same line, but this is up to the person coding

if(1){ return true; }


your welcome

Last edited by dmail; 12-08-2005 at 07:05 PM.
 
Old 12-08-2005, 07:04 PM   #11
byteframe
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 62

Original Poster
Rep: Reputation: 15
I see.

Using derived classes and inheritance wasnt part of this assignment but I thought it was something I'd have fun using, and Im sure I'll learn it properly later, but for right now I't looks like im finally in working order. Thank you very very much, you've been very helpful. Yeah screw windows.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
X include files Gonto Linux - Software 8 01-22-2006 03:42 AM
multiple #include 'Error' Hady Programming 6 02-07-2004 09:53 AM
X include files evian Linux - General 6 03-25-2003 03:53 PM
Multiple Inheritance in Java mikeshn Programming 2 02-25-2002 08:13 PM
RAD tools revisited: Multiple Inheritance and the Web Citizen Bleys Programming 1 02-11-2002 01:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration