W
wenqiang.zhou
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:ate(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
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:ate(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