Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i created this files
C++
//lib.cpp
#include "lib.h"

class myclass
{
    public :
        int value;
};

myclass m1;


and

C++
//lib.h
#ifndef LIB_H_INCLUDED
#define LIB_H_INCLUDED

class myclass;
 extern  myclass m1;

#endif // LIB_H_INCLUDED


main file
C++
#include <iostream>
#include "lib.h"
using namespace std;

int main()
{
    m1.value=10;
    cout<<m1.value;
    return 0;
}


but compiler gives an error in the main file that the m1 object is incomplete type and can not be created.
how can i define a global object of my class and use it in my projects?
Posted
Comments
Sergey Alexandrovich Kryukov 1-Apr-12 15:59pm    
Do you mean global variable of some class? Why?
--SA
Chuck O'Toole 1-Apr-12 20:33pm    
Why not? It's a reasonable thing to do.
Sergey Alexandrovich Kryukov 1-Apr-12 20:46pm    
Well, one of the general principles is to avoid global scope as much as possible, and make the scope of all object as small as possible. In many cases, it's possible to avoid suck objects by using local but static objects, and even stack objects passed all the way from main to all other objects using the resource.

So, if one feels a need in a global object, the first question to ask is "why?". "Is it really necessary?" "Can I avoid it?".

Also, something which may seem fine is a small application, can render a big system cluttered.
--SA

The problem is that the only place the class itself is defined is in the "Lib.CPP" file so noone knows how big it is. The only thing main knowns, via "lib.h" is that there is a class named "myclass" and there is only a partial definition (i.e., it exists) but no other information, therefore an incomplete type.

You'll need to include the full definition of the class in lib.h.

You can only use the "partial definition" (i.e, just the "class myclass;" statement) if you are using "pointers to the class" since the size of a pointer is known regardless of how big the class is. Of course, if you want to use pointers, there are other things you'll have to change.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Apr-12 20:49pm    
Agree, a 5. Still, the first question about global objects should be "why?".
Please see our discussion in the comments to the question.
--SA
Chuck O'Toole 1-Apr-12 20:55pm    
Well, the first thing should be to answer his question. "Why" doesn't even figure into that. But I tend to stay out of religion discussions other than to say I worship at a different set of "general principles".
Sergey Alexandrovich Kryukov 1-Apr-12 22:28pm    
Fair enough. For the record, I've argued in response to your comment, in the comment area, not in the "solution" area. I still think the principle I mentioned is important and see nothing wrong about discussing them. If you want to share any of the principle you "worship" (I personally never remember I would worship anything at all), I would be interested to know what is that. From time to time, people teach me something valuable which I enjoy to arm myself with.

Also, I cannot agree than answering a question is a first thing. I often avoid answering a question when I don't understand why it was asked. It depends, though.

Cheers,
--SA
[no name] 1-Apr-12 20:50pm    
I think correct! 5.
Your main should be,

C++
#include <iostream>
#include "lib.h"
using namespace std;
extern  myclass m1;
int main()
{
    m1.value=10;
    cout<<m1.value;
    return 0;
}
 
Share this answer
 
Comments
Reza Oruji 1-Apr-12 12:24pm    
thanks,but i do not want write this line.
i want objects like cin and cout
Chuck O'Toole 1-Apr-12 20:32pm    
won't fix his problem, m1.value is still unknown at compile time because he does not have the class definiton in lib.h. Lib.H already includes an extern statement, putting in another one won't help at all.
Sergey Alexandrovich Kryukov 1-Apr-12 20:47pm    
Ha-ha, that's true.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900