Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The latest version of this topic can be found at Names with No Linkage.
The only names that have no linkage are:
Function parameters.
Block-scoped names not declared as
externor static.Enumerators.
Names declared in a
typedefstatement. An exception is when thetypedefstatement is used to provide a name for an unnamed class type. The name may then have external linkage if the class has external linkage. The following example shows a situation in which atypedefname has external linkage:// names_with_no_linkage.cpp typedef struct { short x; short y; } POINT; extern int MoveTo( POINT pt ); int main() { }The
typedefname,POINT, becomes the class name for the unnamed structure. It is then used to declare a function with external linkage.
Because typedef names have no linkage, their definitions can differ between translation units. Because the compilations take place discretely, there is no way for the compiler to detect these differences. As a result, errors of this kind are not detected until link time. Consider the following case:
// Translation unit 1
typedef int INT
INT myInt;
...
// Translation unit 2
typedef short INT
extern INT myInt;
...
The preceding code generates an "unresolved external" error at link time.
Example
C++ functions can be defined only in file or class scope. The following example illustrates how to define functions and shows an erroneous function definition:
// function_definitions.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
void ShowChar( char ch ); // Declare function ShowChar.
void ShowChar( char ch ) // Define function ShowChar.
{ // Function has file scope.
cout << ch;
}
struct Char // Define class Char.
{
char Show(); // Declare Show function.
char Get(); // Declare Get function.
char ch;
};
char Char::Show() // Define Show function
{ // with class scope.
cout << ch;
return ch;
}
void GoodFuncDef( char ch ) // Define GoodFuncDef
{ // with file scope.
int BadFuncDef( int i ) // C2601, Erroneous attempt to
{ // nest functions.
return i * 7;
}
for( int i = 0; i < BadFuncDef( 2 ); ++i )
cout << ch;
cout << "\n";
}