|
Only as long as it is defined in another .cpp or.h file that the linker can link against
|
|
|
|
|
Thanks, Dangleberry buddy!
Do you mean "extern" keyword is not useful with functions? I am not quite sure about your meaning.
regards,
George
|
|
|
|
|
Hi,
ny default all functions delareed in header files are extern (giving global access), that is the use of the extern keyword is not needed.
All variables declared in headerfiles are assumed to be internal (are local), that's the why you need to declare global variables as extern.
To make a function local (or internal) you must not use the keyword intern but static!
G. Steudtel
|
|
|
|
|
Thanks, G. Steudtel buddy!
I think variables defined in one header file without "extern" keyword
can be used in another file when you use "#include" keyword.
So, I think what you said "All variables declared in headerfiles are assumed to be internal (are local), that's the why you need to declare global variables as extern." is not correct.
regards,
George
|
|
|
|
|
George2 wrote:
I think variables defined in one header file without "extern" keyword
can be used in another file when you use "#include" keyword.
Are you sure? Please try this:
#include "mod.h"
void main()
{
iGlobalNumber = 2;
}
--------------------------------------
#include "mod.H"
void Test()
{
iGlobalNumber = 1;
}
--------------------------------------
#ifndef _MY_HEADER_FILE_H_
#define _MY_HEADER_FILE_H_
int iGlobalNumber;
#endif // _MY_HEADER_FILE_H_
---------------------------------------
You will get this:
mod.obj : error LNK2005: "int iGlobalNumber" (?iGlobalNumber@@3HA) already defined in main.obj
BuggyMax
|
|
|
|
|
Thanks, BuggyMax buddy!
I have tried youtr exmaple and find you are correct.
But I do not know why the error message is
--------
int iGlobalNumber" (?iGlobalNumber@@3HA) already defined
--------
Why is it "already defined"?
regards,
George
|
|
|
|
|
Well, from the view of the C++ linker, my previous example code would be something like this:
[the whole pseudo code in one single source file]
int iGlobalNumber;
void main()
{
iGlobalNumber = 2;
}
int iGlobalNumber;
void Test()
{
iGlobalNumber = 1;
}
Regarding to the above issue, you may have a look at chapter 9 or 10 (I forgot) in the book "The C++ Programming Language" 3rd edition by Bjarne Stroustrup.
George2 wrote:
(?iGlobalNumber@@3HA)
C++ uses mangled names to serve polymorphism, overloading, etc.
You can take a look at this link: C++ Name Mangling/Demangling[^]
BuggyMax
|
|
|
|
|
Thanks, BuggyMax buddy!
I have added "extern" keyword before int in main.cpp and mod.cpp, but the error still remains the same. How to resolve the trouble?
regards,
George
|
|
|
|
|
The solution is, you can only declare global variables in *.CPP.
[file mod.h]
#ifndef _MY_HEADER_FILE_H_
#define _MY_HEADER_FILE_H_
void Test();
#endif // _MY_HEADER_FILE_H_
--------------------------------
[file main.cpp]
#include "mod.h"
int iGlobalNumber;
void main()
{
iGlobalNumber = 2;
Test();
}
-----------------------------------
[file mod.cpp]
extern int iGlobalNumber;
void Test()
{
iGlobalNumber = 1;
}
------------------------------------
BuggyMax
|
|
|
|
|
Thanks, BuggyMax buddy!
You have corrected another one of my years-old mistakes.
regards,
George
|
|
|
|
|
i am really real newbie when it comes to c++. i just got the "Teach Yourself C++ in 21 Days" book and it is great. there is a sample code in there that i think everyone knows called the "hello world" code. which is:
#include <iostream><br />
<br />
int main()<br />
{<br />
std::cout << "Hello World!\n";<br />
return0;<br />
}
but in viusal C++ 6.0, i tried to build it, but it gives the error:
Compiling...<br />
hello.cpp<br />
c:\documents and settings\owner\desktop\hello.cpp(6) : error C2065: 'return0' : undeclared identifier<br />
c:\documents and settings\owner\desktop\hello.cpp(7) : warning C4508: 'main' : function should return a value; 'void' return type assumed<br />
Error executing cl.exe.<br />
<br />
hello.exe - 1 error(s), 1 warning(s)<br />
i have tried a few other sample codes and they all give me similar errors.
btw, i just installed visual c++ 6.0 standard, and i have made no changes to the settings.
any help would be appreciated. thanks in advance
|
|
|
|
|
Anonymous wrote:
return0;
try:
return 0;
"It was when I found out I could make mistakes that I knew I was on to something."
-Ornette Coleman
|
|
|
|
|
i must have reread and retyped that code 4 times and i did not notice that. thanks so much, parths!
|
|
|
|
|
Personally I think return(0) is clearer.
If I have seen further it is by standing on the shoulders of Giants. - Isaac Newton 1676
|
|
|
|
|
depends if you look at return as a function or statement. Personally I prefer return 0; cos, to me, its more of statement, can you imagine going int length = (0); everywhere? Nah me neither.
|
|
|
|
|
But wouldn't life be boring if we all agreed!
If I have seen further it is by standing on the shoulders of Giants. - Isaac Newton 1676
|
|
|
|
|
That it would be . As long as your consistent with whatever style you prefer, thats the main thing, and I'm sure we'll agree on that .
|
|
|
|
|
Dangleberry wrote:
As long as your consistent
100% in agreement, also whenever I am debugging/changing another persons code I try and use the same style as the original programmer, even if I hate it!
If I have seen further it is by standing on the shoulders of Giants. - Isaac Newton 1676
|
|
|
|
|
how do i create the following :
suppose I have a list box A containing several items and another list box B that will display the item selected in list box A each time the user push a "add" button. It should also undisplay the item when a "remove button" is pushed after selecting the item to be removed from list box B.
|
|
|
|
|
void CYourDialog::OnAddButton()
{
CString cText;
int nIndex;
nIndex = m_ComboboxA.GetCurSel();
if(nIndex == CB_ERR)
return;
m_ComboboxA.GetLBText(nIndex, cText);
m_ComboboxB.AddString(cText);
}
void CYourDialog::OnRemoveButton()
{
int nIndex;
nIndex = m_ComboboxB.GetCurSel();
if(nIndex == CB_ERR)
return;
m_ComboboxB.DeleteString(nIndex);
}
|
|
|
|
|
|
How can I add OnOK event in CPropertySheet
from class wizard?
Thanks in advance.
Shinya
|
|
|
|
|
If file is locked or really user have no permission to write to the file?
Thanks,
Brian
|
|
|
|
|
|
but then again, maybe its simpler than that - I wonder if an attempt to open the file would produce a trappable 'no permissions error' - sorry, I cant check this right now (dont have MSDN here at the office)
'G'
|
|
|
|