By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,508 Members | 1,856 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,508 IT Pros & Developers. It's quick & easy.

puzzle static member & iline

P: n/a
i only kown that static members doesnt blong to any object and it have to
initialize outside class, inline is near the same as define it will replace
in code.but i was puzze by the following codes.

// the first example is the one i predigest from <<the c++ program
language>> written by Bjarne Stroustrup
#include <iostream>
using namespace std;

class Date
{
int d;
static Date default_date;
public:
Date(int dd = 0);
void display(void){cout << d <<endl;};
};

Date::Date(int dd)
{
d = dd?dd:default_date.d ;
};

int main()
{
Date t(4);
t.display();
return 0;
}
--------------------Configuration: 3 - Win32 Debug--------------------
Compiling...
3.cpp
Linking...
3.obj : error LNK2001: unresolved external symbol "private: static class
Date Date::default_date" (?default_date@Date@@0V1@A)
Debug/3.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

3.exe - 2 error(s), 0 warning(s)
************************************************** ************************

#include <iostream>

class AA
{
public:
AA(){cout << "AA" <<endl;};
static int num;
static void handler(void);
};

//int AA::num = 20;//if add this expression ,the code is ok

inline void AA::handler()
{
cout << num <<endl;
}

main ()
{
AA a;
a.handler();
return 0;
}
--------------------Configuration: 3 - Win32 Debug--------------------
Compiling...
5.cpp
C:\5.cpp(18) :error C2065: 'cout' : undeclared identifier
C:\5.cpp(18) :error C2297: '<<' : illegal, right operand has type 'char [3]'
C:\5.cpp(18) :error C2065: 'endl' : undeclared identifier
C:\5.cpp(18) : warning C4552: '<<' : operator has no effect; expected
operator with side-effect
Error executing cl.exe.

3.exe - 3 error(s), 1 warning(s)

************************************************** **************

#include <iostream>

class AA
{
public:
AA(){cout << "AA" <<endl;};
static int num;
static void handler(void);
};

int AA::num = 20;//if add this expression ,the code complite&link is ok
under Dev-Cpp

inline void AA::handler()
{
cout << num <<endl;
}

main ()
{
AA a;
a.handler();
return 0;
}

------------------Configuration: 3 - Win32 Debug--------------------
Linking...
5.obj : error LNK2005: _main already defined in 3.obj
3.obj : error LNK2001: unresolved external symbol "private: static class
Date Date::default_date" (?default_date@Date@@0V1@A)
Debug/3.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

3.exe - 3 error(s), 0 warning(s)
BTW: i find if i add the expression int AA::num = 20 , it will link
complite&link is ok under Dev-Cpp

May 23 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
wenqiang.zhou wrote:
i only kown that static members doesnt blong to any object and it have to
initialize outside class, inline is near the same as define it will
replace in code.but i was puzze by the following codes.

// the first example is the one i predigest from <<the c++ program
language>> written by Bjarne Stroustrup
#include <iostream>
using namespace std;

class Date
{
int d;
static Date default_date;
public:
Date(int dd = 0);
void display(void){cout << d <<endl;};
};

Date::Date(int dd)
{
d = dd?dd:default_date.d ;
};

int main()
{
Date t(4);
t.display();
return 0;
}
--------------------Configuration: 3 - Win32 Debug--------------------
Compiling...
3.cpp
Linking...
3.obj : error LNK2001: unresolved external symbol "private: static class
Date Date::default_date" (?default_date@Date@@0V1@A)
Debug/3.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

3.exe - 2 error(s), 0 warning(s)
************************************************** ************************
Well, the compiler is right. You didn't define Date::default_date. You only
declared it.
#include <iostream>

class AA
{
public:
AA(){cout << "AA" <<endl;};
static int num;
static void handler(void);
};

//int AA::num = 20;//if add this expression ,the code is ok

inline void AA::handler()
{
cout << num <<endl;
}

main ()
{
AA a;
a.handler();
return 0;
}
--------------------Configuration: 3 - Win32 Debug--------------------
Compiling...
5.cpp
C:\5.cpp(18) :error C2065: 'cout' : undeclared identifier
C:\5.cpp(18) :error C2297: '<<' : illegal, right operand has type 'char
[3]' C:\5.cpp(18) :error C2065: 'endl' : undeclared identifier
C:\5.cpp(18) : warning C4552: '<<' : operator has no effect; expected
operator with side-effect
Error executing cl.exe.

3.exe - 3 error(s), 1 warning(s)
cout and endl are in namespace std.
************************************************** **************

#include <iostream>

class AA
{
public:
AA(){cout << "AA" <<endl;};
static int num;
static void handler(void);
};

int AA::num = 20;//if add this expression ,the code complite&link is ok
under Dev-Cpp

inline void AA::handler()
{
cout << num <<endl;
}

main ()
{
AA a;
a.handler();
return 0;
}

------------------Configuration: 3 - Win32 Debug--------------------
Linking...
5.obj : error LNK2005: _main already defined in 3.obj
3.obj : error LNK2001: unresolved external symbol "private: static class
Date Date::default_date" (?default_date@Date@@0V1@A)
Debug/3.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

3.exe - 3 error(s), 0 warning(s)
BTW: i find if i add the expression int AA::num = 20 , it will link
complite&link is ok under Dev-Cpp


Here, you seem to have accidentally linked the first example and this one
together. Since both define a main function, you get an error. The other
error is still the one from the first example.

May 23 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.