external or internal linkage/ is my book wrong?

I'm reading Stephen Prata's C++ primer plus, and he mentions that by default that const variables have internal linkage by default, so const int fingers would be the same as if we declared it as static const int fingers, this seems to be the case for fingers, but the string myName will only work if I explicitly declare it as static, if I don't I will get an error saying myName has been declared more than once.

I'm not sure why this is ok for the integer fingers but not for the string myName?

thanks

Consts.h
1
2
3
4
5
6
7
8
9

#ifndef CONSTS_H_INCLUDED
#define CONSTS_H_INCLUDED

const int fingers = 10;
static const char* myName = "adam";

#endif // CONSTS_H_INCLUDED
  


One.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#ifndef ONE_H
#define ONE_H

#include "Consts.h"

class One
{
    public:
        One();
        void sayNumber();
        void sayName();
        virtual ~One();

    protected:

    private:
};

#endif // ONE_H
  


Two.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#ifndef TWO_H
#define TWO_H
#include "Consts.h"

class Two
{
    public:
        Two();
        void sayInfo();
        virtual ~Two();

    protected:

    private:
};

#endif // TWO_H
  


One.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  #include "One.h"
#include <iostream>

using namespace std;

One::One()
{
    //ctor
}

void One::sayName(){

  cout << myName << endl;

}

void One::sayNumber(){

  cout << fingers << endl;

}

One::~One()
{
    //dtor
}


Two.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include "Two.h"
#include <iostream>

using namespace std;

Two::Two()
{
    //ctor
}

void Two::sayInfo(){

  cout << "name" <<  myName << "number of fingers = " << fingers << endl;
}

Two::~Two()
{
    //dtor
}
  
Last edited on
* and a side question (may as well ask in the same thread as it's related)

when I declare and define num in one.h even if I use extern I get an error saying multiple definitions of num, but when I declare and define num in one.cpp everything works.

One.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#ifndef ONE_H
#define ONE_H

#include "Consts.h"

int num = 15

class One
{
    public:
        One();
        void sayNumber();
        void sayName();
        virtual ~One();

    protected:

    private:
};

#endif // ONE_H
  


Two.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#ifndef TWO_H
#define TWO_H
#include "Consts.h"

class Two
{
    public:
        Two();
        void sayInfo();
        virtual ~Two();

    protected:

    private:
};

#endif // TWO_H
  


One.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  #include "One.h"
#include <iostream>

using namespace std;

One::One()
{
    //ctor
}

void One::sayName(){

  cout << myName << endl;

}

void One::sayNumber(){

  cout << fingers << endl;

}

One::~One()
{
    //dtor
}


Two.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#include "Two.h"
#include <iostream>

using namespace std;

extern int num;

Two::Two()
{
    //ctor
}

void Two::sayInfo(){

  cout << "name" <<  myName << "number of fingers = " << fingers << endl;
}

Two::~Two()
{
    //dtor
}
  


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include "One.h"
#include "Two.h"

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}



would it be because num is defined in main (when one.h is included in main) and defined in one.cpp( when one.h is included in one.cpp)?

thanks
`myName' is not constant, it points to const char
1
2
myName[0] = 'A';
*myName = 'A'
error: assignment of read-only location

myName = nullptr;
perfectly fine

const char * const myName `myName' is constant and points to const char



> when I declare and define num in one.h even if I use extern
you didn't declare it as extern in the header.
#include is simply copy-paste
Last edited on
Topic archived. No new replies allowed.