473,385 Members | 1,875 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.

How to use a variable in separate class when forward declaration fails?

Hey, allow me to make a sketch of the situation:

in foo.cc
Expand|Select|Wrap|Line Numbers
  1. #include "bar.h"
  2. Actor actor;
  3.  
in bar.h
Expand|Select|Wrap|Line Numbers
  1. class Bar {
  2. public:
  3.   void doSomething();
  4. };
  5.  
in bar.cc
Expand|Select|Wrap|Line Numbers
  1. #include "bar.h"
  2.  
  3. Bar::doSomething() {
  4.   actor.action();
  5. }
  6.  
When I compile the code, it will fail because it can't find "actor" in bar. I can't seem to forward declare it, and I can't use a static actor either (I need 'Actor actor' to come after '#include "bar.h"'). Help?

Thanks
Ed
Dec 5 '10 #1

✓ answered by Savage

Take a look at this.

Regards :)

3 2077
Savage
1,764 Expert 1GB
Take a look at this.

Regards :)
Dec 5 '10 #2
Banfa
9,065 Expert Mod 8TB
A forward declaration like Actor actor; just names the type. This allows the compiler to use the type to create pointers and references but does not allow you to access any members of the type (since the compiler does not know about them).

To access the class members you need to have the definition of the class Actor which is presumably in actor.h. You should include this into bar.cc.

Forward declarations are normally used where 2 classes are self referential (that is each class references the other). The forward declaration is normally enough to allow the header of the other class to compile without errors since it normally only contains references and pointers to the first class and the header file for the first class can be included into code files for full access to the class.
Dec 6 '10 #3
extern is exactly what I needed. Thank you both :)
Dec 7 '10 #4

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

Similar topics

2
by: jobseeker | last post by:
From: jobseeker95479@yahoo.com (jobseeker) Newsgroups: comp.lang.c++.moderated Subject: template and forward declaration NNTP-Posting-Host: 131.233.150.22 I have defined a class that contains a...
5
by: Stephane Routelous | last post by:
Hi, I would like to make a forward declaration of a strcuture nested in a class. I have file A.h class A { public: struct B
6
by: Markus Dehmann | last post by:
I have a circular dependency between two classes. The FAQ hint about a forward declaration does not help in my case ( How can I create two classes that both know about each other?) Error:...
1
by: Zeng Dinghao | last post by:
I encouter this problem. here are the sample files: //--------------- File: Bar.h ------------------------------ class Bar : public BarBase { public: void BarFunc(); // there is no BarFunc in...
2
by: Legendary Pansy | last post by:
Hello, I'm trying to accomplish the impossible by trying to do something equivalent of this example found here http://www.c-sharpcorner.com/Code/2003/Dec/DialogTutorial.as Starting with "Listing...
1
by: HappyHippy | last post by:
Hi, I have a question about forward declaration of classes. First of all, a simple working example, which compiles and works with no problems. ///file class_a.h:/ #ifndef __CLASS_A_H__...
1
by: toton | last post by:
Hi, I have two namespace contains class InkFrame and PrefDialog respectively. PrefDialog needs InkFrame to show the dialog over the frame. It also stores a pointer to InkFrame inside it. Now I...
8
by: Mohammad Omer Nasir | last post by:
Hi, i made a structure in header file "commonstructs.h" is: typedef struct A { int i; A( ) {
5
by: fomas87 | last post by:
Hi guys, I'm writing a framework for an sdl gui in c++, but got stuck on the circular dependency, I've read a lot of articles about resolving dependencies and about forward declaration but none of...
4
by: selriel | last post by:
Hi I have an error that's making me go crazy. I'll try to explain it the best I can. I have this class named simMaster -----simMaster.h----- class Hormiga; //Forward declaration clas...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...

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.