473,511 Members | 16,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

instance project

38 New Member
Hi all,

i am having problem by instantiating an object of class in the same source file.
say, we have this code :

// file.h
#ifndef A
#define A

class A{

A();
~A();
// member functions
A_foo();
};
#endif;

// file.cpp
#include file.h

A Obj; // here is the cause of error

void A_foo(){

// something

}

when i execute the program i get segmentation error. If i increment the instance Obj . the program can be executed somehow!!

How can i declare this Obj of the same class file?

may someone help me with any suggestion?
Nov 19 '09 #1
5 1585
weaknessforcats
9,208 Recognized Expert Moderator Expert
You need to include file.h and not file.cpp.

.cpp files are never included.
Nov 19 '09 #2
mar11
38 New Member
you are right. i was in haste.. but that was not my real question!!

do you have any suggestion?
Nov 19 '09 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
Your code won't even compile. How can you possibly be getting a segmentaion fault?

You are creating a global object of type A but your class A methods are all private so there is no way the compiler can call the A default constructor. This is a compile error.

Also, your A class method A_foo has no return type in the class declaration but in the code for the function it returns void.

Lastly, your class name is A and your #ifndef ios also A. That's not going to work.

If you are actually getting this to compile, please get a compiler that works.
Nov 19 '09 #4
mar11
38 New Member
your right!! the code are non-compilable. but that is still not my question.

My question is how can i implement an global object correctly in "the same file structure" as it is written above??



thanks
Nov 20 '09 #5
Banfa
9,065 Recognized Expert Moderator Expert
Well ignoring all the compile errors and missing code what you have in the first post should work so that would suggest the error is in something you chose not to post. Post a compilable example that produces the error and we may be able to be more specific.

Here is a minimal working example
Expand|Select|Wrap|Line Numbers
  1. class GlobalData
  2. {
  3. };
  4.  
  5. GlobalData obj;
  6.  
  7. int main()
  8. {
  9. }
However that aside in C global data was bad practice but not always avoidable, in C++ it is not only bad practice but is generally easily avoidable, I suggest you look up the singleton design pattern
Nov 20 '09 #6

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

Similar topics

2
1213
by: D Witherspoon | last post by:
In my solution I have two projects. One project contains windows forms and other user interface stuff. The other project contains data classes and typed data sets (.xsd). I've referenced...
2
1384
by: gaetanog | last post by:
Hi there.. I have a question about instance usage... I created a VStudio 2003 solution within 3 projects as follow: 1) Proj One: Main form 2) Proj Two: User Control 3) Proj Three: Class I...
0
1145
by: Prakash V via .NET 247 | last post by:
(Type your message here) Hi, I am trying to create a MSProject.Project object instance in VB .Net. this is the code I am using ...
1
2804
by: Chris Magoun | last post by:
I suddenly received an unexpected error in my project. I have been working on this project for some time without this issue. Nothing has changed in the form that caused the exception. A little...
9
5100
by: MrSpock | last post by:
1. Create a new Windows Application project. 2. Open the project properties and check "Make single instance application". 3. Build. 4. Go to the release folder and run the application. 5. Try to...
1
1879
by: otto | last post by:
I have a technical question about WebServices. I have a solution with several projects (.exe and .dll). Each project have references to several webservices. I want to know if is possible to create...
5
1440
by: D Witherspoon | last post by:
What is happening is that I have a class (ClassA) that inherits a class (ClassB) which inherits System.Net.Mail.MailMessage Project 1 references Project 2, Project 2 references Project 3. ...
0
1158
by: Miro | last post by:
I searched google and I think I have solved the issue. Again, if someone could quickly skim over my example and lemmi know if it is the "correct" way of doing things. It works, but again - is it...
2
1592
by: Jordi Julià | last post by:
Hello, I need to create with VB2005 an instance of one of the classes of the project in run time. For example: - The Namespace root and the name of the project are he himself: "Project" -...
45
2960
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to find another way to share an instance of an object with other classes. I started by passing the instance to the other class's constructor, like this: Friend Class...
0
7242
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,...
0
7353
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,...
1
7075
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...
0
7508
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...
0
5662
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,...
1
5063
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...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1572
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 ...
1
781
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.