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

Abstract class linking issue

First question - let's get this out of the way since it might be the
solution to all my woes: Does it make sense to have a .cpp file for a
class that is declared as having pure virtual functions in its .h file?

Here's my predicament. I'll put this in general terms since it's
happening across several classes.

I have a base class, let's say it's named A. In A.h I DECLARE several
pure virtual ( = 0) functions, a virtual destructor, and a constructor.
Note that each of these is terminated with a semicolon, hence they're
not DEFINED. There is also a file A.cpp which defines the constructor
and destructor with empty curly braces. A.cpp and A.h compile into a
library file (.a) with GCC without errors.

There's a class B that derives class A. All of the pure virtual
functions are overridden by B. Class B also has a default constructor
(no parameters) and destructor. Class B compiles into a library
without error also.

When I go to link my executable I include the two resulting libraries.
The linker produces the error "undefined reference to A::A()" which is
said to occur in the constructor for class B. I've ensured that the
folders containing A.h and B.h are in the "include search path", and
I've ensured that the libraries containing A and B are included during
linking.

Oct 11 '06 #1
4 2869
Richard wrote:
First question - let's get this out of the way since it might be the
solution to all my woes: Does it make sense to have a .cpp file for a
class that is declared as having pure virtual functions in its .h
file?
Sure, why not? If you want to provide definitions for the pure virtual
functions, you may. It's especially useful if you ever call any of the
functions from a c-tor of that class.
Here's my predicament. I'll put this in general terms since it's
happening across several classes.

I have a base class, let's say it's named A. In A.h I DECLARE several
pure virtual ( = 0) functions, a virtual destructor, and a
constructor. Note that each of these is terminated with a semicolon,
hence they're not DEFINED. There is also a file A.cpp which defines
the constructor and destructor with empty curly braces. A.cpp and
A.h compile into a library file (.a) with GCC without errors.
OK. Just so we are straight here, "compiles into a library" is not
defined in C++.
There's a class B that derives class A. All of the pure virtual
functions are overridden by B. Class B also has a default constructor
(no parameters) and destructor. Class B compiles into a library
without error also.
OK
When I go to link my executable I include the two resulting libraries.
Linking with libraries isn't defined either. It's known to work in
many compilers, but on every compiler that operation is specific to
the implemenation.
The linker produces the error "undefined reference to A::A()" which is
said to occur in the constructor for class B. I've ensured that the
folders containing A.h and B.h are in the "include search path", and
I've ensured that the libraries containing A and B are included during
linking.
Does it happen if you add all those files to the same project instead
of using "resulting libraries"? If it does not happen, then your
problem is not of C++ nature, sorry. If it does happen even if your
classes are both parts of the same _source_ file, let's see it.

For implementation-specific behaviour you need to ask in the newsgroup
dedicated to your compiler.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 11 '06 #2
Victor Bazarov wrote:
Richard wrote:
>First question - let's get this out of the way since it might be the
solution to all my woes: Does it make sense to have a .cpp file for a
class that is declared as having pure virtual functions in its .h
file?

Sure, why not? If you want to provide definitions for the pure virtual
functions, you may. It's especially useful if you ever call any of the
functions from a c-tor of that class.
>Here's my predicament. I'll put this in general terms since it's
happening across several classes.

I have a base class, let's say it's named A. In A.h I DECLARE several
pure virtual ( = 0) functions, a virtual destructor, and a
constructor. Note that each of these is terminated with a semicolon,
hence they're not DEFINED. There is also a file A.cpp which defines
the constructor and destructor with empty curly braces. A.cpp and
A.h compile into a library file (.a) with GCC without errors.

OK. Just so we are straight here, "compiles into a library" is not
defined in C++.
>There's a class B that derives class A. All of the pure virtual
functions are overridden by B. Class B also has a default constructor
(no parameters) and destructor. Class B compiles into a library
without error also.

OK
>When I go to link my executable I include the two resulting libraries.

Linking with libraries isn't defined either. It's known to work in
many compilers, but on every compiler that operation is specific to
the implemenation.
>The linker produces the error "undefined reference to A::A()" which is
said to occur in the constructor for class B. I've ensured that the
folders containing A.h and B.h are in the "include search path", and
I've ensured that the libraries containing A and B are included during
linking.

Does it happen if you add all those files to the same project instead
of using "resulting libraries"?
"add all those files to the same project" is not defined in C++.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Oct 11 '06 #3
Pete Becker wrote:
Victor Bazarov wrote:
>Richard wrote:
[...]
>>The linker produces the error "undefined reference to A::A()" which
is said to occur in the constructor for class B. I've ensured that
the folders containing A.h and B.h are in the "include search
path", and I've ensured that the libraries containing A and B are
included during linking.

Does it happen if you add all those files to the same project instead
of using "resulting libraries"?

"add all those files to the same project" is not defined in C++.
Something crawled up somewhere this fine morning, eh?

Replace 'project' with 'program'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 11 '06 #4
It turned out to be a linking order issue. I modified the order in my
Makefile and resolved all the "undefined reference" errors.

Oct 12 '06 #5

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

Similar topics

9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
6
by: Steve | last post by:
I am designing a new class hierarchy and I've already run into a bit of a bump in the road. I have this structure so far; class CodeGen class CodeGenHeader : CodeGen class CodeGenProtocolHeader...
15
by: Pohihihi | last post by:
How can I overload abstract type method in child class? e.g. public abstract void BaseMethod() { // do something } // in child class
4
by: sudhir | last post by:
Q 1. I defined a class with 10 functions . It contains declaration of 5 functions and 5 functions are declared and defined. Is this class is said to a abstract class ? Q 2. Which one is...
4
by: skishorev | last post by:
and what is object delagation, and how it can implemented?
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
2
by: Zytan | last post by:
I know that WebRequest.GetResponse can throw WebException from internet tutorials. However in the MSDN docs: http://msdn2.microsoft.com/en-us/library/system.net.webrequest.getresponse.aspx It...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.