puzzle static member & iline

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::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
 
R

Rolf Magnus

wenqiang.zhou said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top