473,386 Members | 1,798 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,386 software developers and data experts.

Problems accessing methods of derived class

I have a base class and a derived class, but I am getting errors when I
try to access functions of the derived class.

Simplified version of my code is as follows:

////////////////
// test2.hh

class BaseClass {

public:
BaseClass();
~BaseClass();

};

class DerivedClass : public BaseClass {

public:
DerivedClass() {}
~DerivedClass() {}

void setup();

};

////////////////
// test2.cc

#include "test2.hh"

int main() {

BaseClass* object;

object = new DerivedClass();
object->setup();

return 0;
}
When I try to compile this, I get the error:
test2.cc:8: error: `setup' undeclared (first use this function)
If I change the line to say:
object->DerivedClass::setup();

I get the error:
test2.cc:8: error: type `DerivedClass' is not a base type for type
`BaseClass'
And if I change the pointer declaration to say:
DerivedClass* object;

I get the linker error:
/tmp/ccaPvY8l.o: In function `main':
test2.cc:(.text+0x68): undefined reference to `DerivedClass::setup()'
/tmp/ccaPvY8l.o: In function `DerivedClass::DerivedClass[in-charge]()':
test2.cc:(.gnu.linkonce.t._ZN12DerivedClassC1Ev+0x d): undefined
reference to `BaseClass::BaseClass[not-in-charge]()'
collect2: ld returned 1 exit status
Can I change the declaration of DerivedClass so that I can use it as in
the code given? Or am I going about this the wrong way completely?

Aug 31 '06 #1
2 2612

Jessica wrote:
I have a base class and a derived class, but I am getting errors when I
try to access functions of the derived class.

Simplified version of my code is as follows:

////////////////
// test2.hh

class BaseClass {

public:
BaseClass();
~BaseClass();

};

class DerivedClass : public BaseClass {

public:
DerivedClass() {}
~DerivedClass() {}

void setup();

};

////////////////
// test2.cc

#include "test2.hh"

int main() {

BaseClass* object;

object = new DerivedClass();
object->setup();

return 0;
}
When I try to compile this, I get the error:
test2.cc:8: error: `setup' undeclared (first use this function)
You don't declare it in base class, of course you cann't use it
>
If I change the line to say:
object->DerivedClass::setup();

I get the error:
test2.cc:8: error: type `DerivedClass' is not a base type for type
`BaseClass'
This syntax is for you to call the function defined in base.
>
And if I change the pointer declaration to say:
DerivedClass* object;

I get the linker error:
/tmp/ccaPvY8l.o: In function `main':
test2.cc:(.text+0x68): undefined reference to `DerivedClass::setup()'
/tmp/ccaPvY8l.o: In function `DerivedClass::DerivedClass[in-charge]()':
test2.cc:(.gnu.linkonce.t._ZN12DerivedClassC1Ev+0x d): undefined
reference to `BaseClass::BaseClass[not-in-charge]()'
collect2: ld returned 1 exit status
This is because you only defined the function but not define it
>
Can I change the declaration of DerivedClass so that I can use it as in
the code given? Or am I going about this the wrong way completely?
Yeah, you have to study more carefully on c++

Just declare a setup() in Base class as a virtual function, you'll get
what you want

Aug 31 '06 #2
Jessica wrote:
I have a base class and a derived class, but I am getting errors when I
try to access functions of the derived class.

Simplified version of my code is as follows:
< clipped>
>
int main() {

BaseClass* object;

object = new DerivedClass();
object->setup();

return 0;
}
OK, you are going to learn something today.

If you plan to use a pointer to a base class, you are attempting to
implement a polymorphic solution. That means that you have to equip
your classes with a vtable. The way this is done is simply by declaring
at least one function as virtual. In this case, a virtual destructor
will suffice. That means that you constructing a C++ program, not C.

Also, if the plan is to reuse these various classes, declare them in
their own headers with include guards. So you'll need 2 header files, 2
corresponding implementation files (which i'm skipping here) + an
implementation file for main(). My guess is that you are looking to
make setup() virtual as well.

Note that #ifndef, #define and #endif are used to supply header guards
in order to prevent your linker from including the same header file
multiple times. For all intensive purposes, you can ignore include
guards when you are reading these classes. Include guards are only for
the linker.

////////////////
// baseclass.h

#ifndef BASECLASS_H_
#define BASECLASS_H_

class BaseClass
{

public:
BaseClass() { }
virtual ~BaseClass() { }
virtual void setup() { }
};

#endif /* BASECLASS_H_ */

////////////////
// derivedclass.h

#ifndef DERIVEDCLASS_H_
#define DERIVEDCLASS_H_

#include "baseclass.h"

class DerivedClass : public BaseClass
{
public:
DerivedClass() { }
~DerivedClass() { } // virtual
void setup() { } // virtual
};

#endif /* DERIVEDCLASS_H_ */

////////////////
// proj_test.cpp

#include "derivedclass.h"

int main()
{
BaseClass* object;
object = new DerivedClass;
object->setup();
delete object;

return 0;
}

Note that DerivedClass's destructor and DerivedClass's setup() are
virtual because of inheritance.

>
When I try to compile this, I get the error:
test2.cc:8: error: `setup' undeclared (first use this function)
Thats because you never provided setup's implementation.
void setup(); // is a declaration with no implementation (yet)
void setup() { } // is a declaration with implementation which doesn't
do anything right now
>

If I change the line to say:
object->DerivedClass::setup();

I get the error:
test2.cc:8: error: type `DerivedClass' is not a base type for type
`BaseClass'
Their isn't any vtable available and DerivedClass is not a base class.
>

And if I change the pointer declaration to say:
DerivedClass* object;

I get the linker error:
/tmp/ccaPvY8l.o: In function `main':
test2.cc:(.text+0x68): undefined reference to `DerivedClass::setup()'
/tmp/ccaPvY8l.o: In function `DerivedClass::DerivedClass[in-charge]()':
test2.cc:(.gnu.linkonce.t._ZN12DerivedClassC1Ev+0x d): undefined
reference to `BaseClass::BaseClass[not-in-charge]()'
collect2: ld returned 1 exit status
The compiler is basicly telling you that their is no base class
available with an implementation of setup() available to compile with.

Note that you can seperate your declaration and implementation like
so...

////////////////
// derivedclass.h

#ifndef DERIVEDCLASS_H_
#define DERIVEDCLASS_H_

#include "baseclass.h"

class DerivedClass : public BaseClass
{
public:
DerivedClass();
~DerivedClass();
void setup();
};

#endif /* DERIVEDCLASS_H_ */

////////////////
// derivedclass.cpp

#include "derivedclass.h"

DerivedClass::DerivedClass()
{
}

DerivedClass::~DerivedClass()
{
}

void DerivedClass::setup()
{
}

/* end of derivedclass.cpp */
>

Can I change the declaration of DerivedClass so that I can use it as in
the code given? Or am I going about this the wrong way completely?
To implement a polymorphic solution, you need to compile a C++ program
with the base class equipped with a vtable (at least one virtual
function - a virtual destructor wiil do)

Just for the sake of clarity, try compiling the following and carefully
note which function gets called and what destructors get invoked. Then
try compiling the same program without the virtual keywords (virtual is
the key). Remember: virtual is what makes the vtable.

// proj_test.cpp
//

#include <iostream>
#include <ostream>

class Base
{
public:
Base() { std::cout << "Base()\n"; }
virtual ~Base() { std::cout << "~Base()\n"; }
virtual void setup() { std::cout << "Base::setup()\n"; }
};

class Derived : public Base
{
public:
Derived() { std::cout << "Derived()\n"; }
~Derived() { std::cout << "~Derived()\n"; }
void setup() { std::cout << "Derived::setup()\n"; }
};

int main()
{
std::cout << "___ Base pointer, Derived object ___\n";
Base* object;
object = new Derived;
object->setup();
delete object;

std::cout << "\n___ Base pointer, Base object ___\n";
Base* base;
base = new Base;
base->setup();
delete base;

return 0;
}

/*

___ Base pointer, Derived object ___
Base()
Derived()
Derived::setup()
~Derived()
~Base()

___ Base pointer, Base object ___
Base()
Base::setup()
~Base()

*/

Aug 31 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: John M. Gabriele | last post by:
I've done some C++ and Java in the past, and have recently learned a fair amount of Python. One thing I still really don't get though is the difference between class methods and instance methods. I...
1
by: Bryan Ray | last post by:
I am trying to write an inheritance function, so I can call a base classes method that has been overridden in the derived class. I want to get rid of the ugly 'call()' syntax that would be used....
5
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the...
2
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
1
by: Adam Clauss | last post by:
First, I have ComVisible set to true for the entire assembly. I have (abstract) base class A. There are interfaces IA and IAEvents for accessing properties of this object and exposing events. ...
2
by: Ramashish Baranwal | last post by:
Hi, I want to access a static variable in a staticmethod. The variable can be redefined by derived classes and that should be reflected in base's staticmethod. Consider this trivial example- ...
4
by: softwaredoug | last post by:
Here is some test code I've been attempting to compile (Visual Studio 2003) test.h: class Base { protected: Base() {} public:
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.