hi all,
I was trying the following code on the MS .net compiler:
class Item {
static int count;
public:
//static void init() { }
Item()
{
count++;
cout<<"construc ting Item"<<endl;
}
~Item()
{
count--;
cout<<"Destroyi ng Item #"<<count<<endl ;
}
};
The compiler kept giving me error for the unresloved symbol count.
After that , I made the variable count public and tried to assign 0 to
it in the main function. But still the same error was reported by the
compiler. I am confused about why the error has occured.
Thanks,
Madhav. 14 5933
Madhav wrote:
hi all,
I was trying the following code on the MS .net compiler:
class Item {
static int count;
public:
//static void init() { }
Item()
{
count++;
cout<<"construc ting Item"<<endl;
}
~Item()
{
count--;
cout<<"Destroyi ng Item #"<<count<<endl ;
}
};
The compiler kept giving me error for the unresloved symbol count.
After that , I made the variable count public and tried to assign 0 to
it in the main function. But still the same error was reported by the
compiler. I am confused about why the error has occured.
Do you have a definition of Item::count in a source file? Sounds like
you have forgotten to do this.
--
Ian Collins.
try to make initialization in this manner in your constructor:
Item() :count(0)
{
count++;
cout<<"construc ting Item"<<endl;
}
this manner is called the initializer list.
Note that you can't initialize constant members in a class by =
operator, instead you should use initializer list.
if there are many variables that you want to initialize feel free to
use comma, i.e.
Item():count1(o ), count2(3), count3(9)
{
count1++;
count2++;
count3++;
}
luckyboy wrote:
try to make initialization in this manner in your constructor:
Item() :count(0)
No, this is wrong.
If you look back to the code you snipped, count is a static member. It
requires a definition.
--
Ian Collins.
Yes, you must write " int Item::count=0; " in the source file (normal in
the .cpp file).
"Ian Collins" <ia******@hotma il.comwrite
news:4i******** ****@individual .net...
Madhav wrote:
hi all,
I was trying the following code on the MS .net compiler:
class Item {
static int count;
public:
//static void init() { }
Item()
{
count++;
cout<<"construc ting Item"<<endl;
}
~Item()
{
count--;
cout<<"Destroyi ng Item #"<<count<<endl ;
}
};
The compiler kept giving me error for the unresloved symbol count.
After that , I made the variable count public and tried to assign 0 to
it in the main function. But still the same error was reported by the
compiler. I am confused about why the error has occured.
Do you have a definition of Item::count in a source file? Sounds like
you have forgotten to do this.
--
Ian Collins.
recover wrote:
Yes, you must write " int Item::count=0; " in the source file (normal in
the .cpp file).
Please don't top post!
"Ian Collins" <ia******@hotma il.comwrite
news:4i******** ****@individual .net...
>>Madhav wrote:
>>>hi all, I was trying the following code on the MS .net compiler:
class Item { static int count;
public: //static void init() { }
Item() { count++; cout<<"const ructing Item"<<endl; } ~Item() { count--; cout<<"Destr oying Item #"<<count<<endl ; } };
The compiler kept giving me error for the unresloved symbol count. After that , I made the variable count public and tried to assign 0 to it in the main function. But still the same error was reported by the compiler. I am confused about why the error has occured. Do you have a definition of Item::count in a source file? Sounds like you have forgotten to do this.
-- Ian Collins.
--
Ian Collins.
recover wrote:
Yes, you must write " int Item::count=0; " in the source file (normal
in
the .cpp file).
Please don't top post!
Thanks. I will be careful of.
I use Outlook Express to reply news. It's template take me to wite at begin
of letter.
Ian Collins wrote:
luckyboy wrote:
try to make initialization in this manner in your constructor:
Item() :count(0)
No, this is wrong.
If you look back to the code you snipped, count is a static member. It
requires a definition.
Thanx Ian Collins., this really helped me, but i wanna mention that i
still beginner in C++ field and just i wanna help others to solve their
problems. Again Thank you :)
Ian Collins wrote:
luckyboy wrote:
try to make initialization in this manner in your constructor:
Item() :count(0)
No, this is wrong.
If you look back to the code you snipped, count is a static member. It
requires a definition.
--
Ian Collins.
hey Ian,
I wrote this in the main function by making the count
variable public:
int Item::count=0;
But now the compiler gives me something like:
error C2655: 'Item::count' : definition or redeclaration illegal in
current scope
see declaration of 'Item::count'
Then I moved the declaration above the main function and
everything worked fine. Can you please explain why it did not work in
previous case and works in this case?
I am not sure if the static variables follow the rules of
global variables for initialization. But I think this may just be it.
Regards,
Madhav.
Madhav wrote:
Please try and keep some context, you should have replied to my first
posting.
>
hey Ian,
I wrote this in the main function by making the count
variable public:
int Item::count=0;
There's your mistake, the definition should be in the global scope (or
the same namespace as the class), not in main.
But now the compiler gives me something like:
error C2655: 'Item::count' : definition or redeclaration illegal in
current scope
see declaration of 'Item::count'
It's telling you you can't put the definition in main.
Then I moved the declaration above the main function and
everything worked fine. Can you please explain why it did not work in
previous case and works in this case?
I think I just have!
--
Ian Collins. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Andy |
last post by:
1) Is there any use of defining a class with a single constructor
declared in private scope? I am not asking a about private copy
constructors to always force pass/return by reference.
2) Is this in any way used to create singletons. Can someone say how?
Cheers,
Andy
|
by: Neno |
last post by:
Hi,
I have a linkage error that has something to do with the use of a static
member array and I can't understand how to solve the problem. Can someone
help me please?
In detail:
A class Month (it is just an unsafe and useless example) has protected
constructors so that Month objects can be obtained only through a static
member function named getMonth.
Month class has a private static array named months made of 12 pointers to
Month...
|
by: baumann |
last post by:
hi all,
according the private / protected access control,
- private; that is, its name can be used only by members and friends
of the class in which it is
declared.
- protected; that is, its name can be used only by members and
friends of the class in which it is
|
by: Diebels |
last post by:
Hi,
I have some problems using static variables which results in a core
dump. I have attached code and coredump to the end of my message.
I am trying to implement a kind of factory design. I have a base class
with several sub classes. In runtime I want to create a instance of a
sub class and assign it to a base class pointer. Nothing fancy about
that. I also want to be able in runtime to decide witch type of sub
class that is to be...
|
by: jimmij |
last post by:
Hi,
Please look at the code bellow
/*******************/
class ctab
{
private:
static const unsigned n=48;
| |
by: Joe Van Dyk |
last post by:
class Foo
{
public:
Foo() : smart_ptr_()
{
// reads some data
int param = read_some_data();
// based off this data, we can create a Something object
smart_ptr_ = smart_pointer<Something>(new Something(param));
}
|
by: sunil |
last post by:
Hello,
I am new to c# . I have some basic programming doubts. Please help me
in clarifying these doubts.
I want to initialize a static and readonly field with a value returned
by a static method. How ever, when I am debugging, that method is not
being called. So I am not able to figure out, whether the field is
getting initialized properly or not. Please explain this behavior
Thanks in advance
|
by: John |
last post by:
Hello,
is there any compiler option for g++ for initializing static members of the
class.
Due to some unknown reason, static member in one of our c++ application is
not getting initialized properly.
Please help me on this.
Thanks,
|
by: Grey Alien |
last post by:
class A
{
public:
A(const B& ref);
private:
static B& b ;
};
How may b be initialized ?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
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();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |