0

Do classes have external linkage? If so, why couldn't I get the following to work on gcc?

// mycpp1.cpp

class MyClass
{
    int x;
    // ......
};

`

// mycpp2.cpp

class MyClass; // why doesn't this work?

I've done some googling around and came around this,

C++ Standard

(3.5/4) a named class (clause 9), or an unnamed class defined in a typedef declaration in which the class has the typedef name for linkage purposes (7.1.3);

I could just include the file, but what would be the purpose of linkage?

user103214
  • 3,478
  • 6
  • 26
  • 37

3 Answers3

2

You should be doing this:

  • Header File : declaration should go in .h file

    // mycpp1.h 
    class MyClass
    {
       int x;
       void f(); //only declaration
    };
    
  • Source File : : definition should go in .cpp file

     // mycpp1.cpp
     #include "mycpp1.h"
    
     void MyClass::f() //definition of member function
     {
        //code
     }
    

and then use MyClass as

// mycpp2.cpp
#include "mycpp1.h"

MyClass instance; // it should work!

I would suggest you to pick a good introductory book on C++. Here is a comprehensive list of good books on C++:

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
2

Why I half agree with Nawez that you shouldn't worry too much about linkage, it is nice to understand it, so you know why the coding pattern you're using works (and why other patterns don't). Linkage defines how a name (a symbol) is bound to an entity (variable, function, type, etc.). External linkage means that the name (or the qualified version of it) binds to the same entity in all of the translation units: in your case, that MyClass is the same class in all of the translation units. It doesn't mean that all translation units automatically know how it is defined; just that MyClass in mycpp1.cpp is the same type as MyClass in mycpp2.cpp. For various reasons related to compiler and linker technology, you still have to provide a definition in every source (preferably by means of an include) that uses it, but these definitions are required to be identical (which is why the include is preferred), because the compiler will generate code treating them as identical.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • And I half-disagree with this : *"Why I half agree with Nawez that you shouldn't worry too much about linkage"*. I didn't say that only, I also added, "for the time being". As for *" it is nice to understand it"*, everything in C++ is nice to understand for a C++ programmer. – Nawaz Dec 01 '11 at 11:14
  • @Nawaz: Why did you assume that I don't know what you showed in your answer? Anyway, I won't argue if you can't or can or if you don't wish to explain that :-) – user103214 Dec 01 '11 at 11:23
  • 2
    @user974191: It is pretty much obvious you don't know the basic. – Nawaz Dec 01 '11 at 11:27
1
class MyClass; // why doesn't this work?

This is a forward declaration - it declares that the class exists, but doesn't provide the information needed to do much with it.

With just a forward declaration, you can do a few things, including declaring a pointer or reference to it, and declaring a function with it as an argument or return value. You can't instantiate it, inherit from it, access any of its members, or do various other things without the full definition, for which you need to include the header file.

I could just include the file, but what would be the purpose of linkage?

A header file often contains declarations of functions and objects, which can then be fully defined in a separate source file and not included by files using them. That is the purpose of linkage, but it doesn't extend to allowing class definitions to be in separate source files.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644