473,657 Members | 2,516 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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._ZN1 2DerivedClassC1 Ev+0xd): undefined
reference to `BaseClass::Bas eClass[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 2634

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._ZN1 2DerivedClassC1 Ev+0xd): undefined
reference to `BaseClass::Bas eClass[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._ZN1 2DerivedClassC1 Ev+0xd): undefined
reference to `BaseClass::Bas eClass[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.cp p

#include "derivedclass.h "

DerivedClass::D erivedClass()
{
}

DerivedClass::~ DerivedClass()
{
}

void DerivedClass::s etup()
{
}

/* end of derivedclass.cp p */
>

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
6945
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 guess I'll try to narrow it down to a few specific questions, but any further input offered on the subject is greatly appreciated: 1. Are all of my class's methods supposed to take 'self' as their first arg? 2. Am I then supposed to call...
1
1728
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. Ideally I would like to call the method using syntax such as: object.base.method() The idea is to use an object ('base') to provide the base classes properties and proxy methods to the real methods stored in '_class' - see the function below....
5
3645
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 parent class, and I can't make some "helper" class a friend of the parent class to help in accessing the data.) Problem: I want to derive a class that has a copy constructor that properly copies those data members.
2
2297
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 in Rectangle, the compiler tells me Point::x is protected. I would have expected Rectangle to see the protected members of any Point. Compiling the following code give me this error: g++ -o rectangle main.cc main.cc: In member function `size_t...
32
4507
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
2
3169
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 problems that I have encountered to date and the solutions (if any) that I found. http://users.adelphia.net/~brianpclab/ServerControlCollectionIssues.htm This page also has all of the source code in a compressed file that you are free to download...
1
1570
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. [ClassInterface(ClassInterfaceType.None), ComSourceInterfaces(typeof(IAEvents)) public abstract class A : IA {
2
2946
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- class Base: staticvar = 'Base' @staticmethod
4
2938
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
8420
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8842
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8516
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7353
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.