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

C++ brushing-up..., class forward declarations Q?

2
Hi Bytes Community,

I've been away from C++ programming for some years, and now find me self in need to recompile old code that compiles under g++ 2.96, but not under g++ 3.4.3. I notice that g++ 3.4 changed the parser to be more conforming and thus I now see compile errors like tried illustrated below. I've been googlling around, but so far haven't understood the issue at hand, hoping for hints from someone here.

Got code like this:

line n+1: class classA; // forward declaration

line n+3: typedef templateClassC <classA> classD;

line n+5: class classB : public classD {
line n+6 virtual A *ptr2A() {};
line n+7 }


gives me compile time errors like:

line n+5: instantiated from here
line n+1: error: forward declaration of `struct classA'

Why it g++ 3.4.3 considering the forward declaration as been of a struct and not a class type (assuming my code in non-conforming, but how to resolve this chicken/egg issue)?

TIA
Jan 21 '10 #1
3 1805
weaknessforcats
9,208 Expert Mod 8TB
This code:

Expand|Select|Wrap|Line Numbers
  1. line n+1: class classA; // forward declaration
  2.  
  3. line n+3: typedef templateClassC <classA> classD;
shows a class A as a template argument. To use an A you will need a constructor for A and a forward declaration does not provide that.

You can use a forward declaration for A only to justify using an A* as an argument somewhere. That is:

Expand|Select|Wrap|Line Numbers
  1.  typedef templateClassC <classA*> classD;
would be OK provided there's not somethin in the template that requires the compiler to know about the entire classA.

Your solution here is to remove the forward referention and #include the classA class declaration instead.
Jan 21 '10 #2
stws
2
Thanks and sorry see now I forgot to tell classA references classB as well and thus give the chicken&egg problem. So one class needs to be defined before the other and as said 2.96 been less strict conforming compiled the code. Chicken&eggs still exists even with stricter compiler/parsers I assume, how do one work around such then?

Did try to work a bit on using include files instead as I read a bit here and on gcc.gnu.org, only no better so far.
Jan 21 '10 #3
weaknessforcats
9,208 Expert Mod 8TB
As I said, you have to use a pointer to the class.

That is, if class A references class B and class B references class A then in each case, the contructor for A must be known by the compiler to compile class B and vice versa for class A. You are now dead.

You make this scenario work by having one of the classes use a pointer rather than a reference. Using a pointer does not require a constructor. Just knowing the class name is enough for the compiler to allow a pointer to an object of that class. More than that and the compiler will need to see the class declaration.


Expand|Select|Wrap|Line Numbers
  1. class A;
  2. class B
  3. {
  4.       A*  obj;
  5.       //etc...
  6.  
  7. };
Jan 22 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

38
by: Jukka K. Korpela | last post by:
As well all know, valid markup is important... but when trying to find a convincing modern argument in favor of this, I found pages like http://www.htmlhelp.com/tools/validator/reasons.html which...
133
by: Philipp Lenssen | last post by:
Why is there no standardized and well-working way for a web-page to offer the font for download/embed it, in order to be displayed on the page? No matter what you think of the preferred font of a...
21
by: Florian Brucker | last post by:
Hi! With the help of this group I finally managed to transfer my website from table layout to CSS layout. The site is now online and I would really appreciate comments about it. I'm...
24
by: simon | last post by:
Hi, First some background. I have a structure, struct sFileData { char*sSomeString1; char*sSomeString2;
28
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
15
by: AussieRules | last post by:
Hi, I have a need to do two selects against to stored proc's in my SQL db. At the moment, each SP is called and two different dataset are populate. Thats two round trips to the SQL server. ...
10
by: Eva | last post by:
Does anyone know how i can connect my combobox to my acces DB? i have tried setting the datasource to a dataset but i cannot seem to locate the item property under the DataBindings list. I ended...
4
by: marion philpott | last post by:
Has anyone put together their own access database for genealogy, I am thinking of doing one myself but have only basic level of access , would anyone have their own , who would be willing to send...
9
by: farseer | last post by:
Hi, Using a disassembler, i am able to see the exported functions in a dll. My question is, how do i make a call to those functions and compile my code if i do not have the header?
0
by: vaticans.org | last post by:
Grooming Your Dog Grooming is an important part of your dog's health, with regular brushing and combing helping to remove dead hair and dirt and prevent matting. Dogs who are regularly groomed...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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: 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.