473,725 Members | 2,197 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2897
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
4646
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
3345
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 near-impossible. It seems like it would be useful. In fact, there's a place in my code that I could make good use of it. So why not? Chris
9
5199
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
1699
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 : CodeGenHeader CodeGen is not an entirely abstract class, there are properties like FileName, Date, etc that are shared with all class', however there are other members that although they are shared with all the derived classes, need to
15
2648
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
2344
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 correct? If a class contains a virtual class then it is said to a abstract class ? Or a abstract class always contains a virtual function ?
4
3924
by: skishorev | last post by:
and what is object delagation, and how it can implemented?
7
4471
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 to be checking that the correct data type is used DataAcess sets an abstract class and methods
2
2966
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 only lists NotImplementedException in the Exceptions section. (Note that it does mention WebException in the Remarks section, but who knows if this is always the case for such classes, and thus perhaps they can't be trusted to always list these, and...
0
9401
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
9257
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
9176
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
9113
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...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.