no extern declaration of an intance possible?

Hi I want to create an global object of a class,
is this possible?

for a normal primitv type i can just declaire it as

 
  extern int a;


But if i try it with an object of a class like:

 
  extern class_one* test_one = new class_one;


The compiler quits with the error messages C4430 and C2143.

C4430 (assuming you're using Visual Studio), all declarations must explicitly specify the type; int is no longer assumed.

That would imply that class_one has not been declared at the time the extern is processed by the compiler.


Yes it is VS I did it this way:

The Class as a cpp file
1
2
3
4
5
6
7
8
9
10
#include"stdafx.h"
#include "class_one.h"

void class_one::set_v(int x) {
	b = x;
}
void class_one::print() {
	std::cout << "a: " << a << std::endl;
	std::cout << "b: " << b << std::endl;
}


The Class as a h file
1
2
3
4
5
6
7
8
9
10
11
12
#pragma once
#include"stdafx.h"

class class_one {
private:
	int const a = 7;
	int b;

public:
	void set_v(int x);
	void print();
};


Than the extern declairation
1
2
3
4
5
#include"stdafx.h"
#pragma once

extern int a;
extern class_one* test_one;


and the main cpp file
1
2
3
4
5
6
7
8
9
10
11
#include "stdafx.h"

int main()
{
	std::cout << "test:" << a << std::endl;
	a++;
	std::cout << "test:" << a;
		
	system("pause");
    return 0;
}


It is just to test how to declaire variables as exter for training reason.
Last edited on
Where are those extern declarations located? It looks like they're in some header file so where are you #including that header file. And also you need to define those variables in one and only one source file without the extern qualifier.

header.h
1
2
3
4
#pragma once
extern int global_a;

void someFunction();


functions.cpp
1
2
3
4
5
6
7
8
#include <iostream>

#include "header.h"

void someFunction()
{
    std::cout <<   global_a << std::endl;
}


main.cpp

1
2
3
4
5
6
7
8
9
10
11
#include "header.h"

int global_a;

int main()
{
    global_a = 100;
    someFunction();

    return 0;
}
extern simply tells the linker the variable exists in this or another compilation unit. You don't actually define a or test_one anywhere.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "stdafx.h"
#include "extern.h"  // include extern declarations

int a = 0;  // You must actually define a somewhere
class_one * test_one = new class_one;  // Same goes for class_one pointer

int main()
{      std::cout << "test:" << a << std::endl;
	a++;
	std::cout << "test:" << a;		
	system("pause");
        return 0;
}

Last edited on
ex_declaration.h
1
2
3
4
5
#include"stdafx.h"
#pragma once

extern int a;
extern class_one* test_one;


ex_declaration.cpp
1
2
3
4
#include "stdafx.h"

int a = 7;
class_one* test_one = new class_one;


and in the stdafx.h
1
2
3
4
5
6
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <conio.h>
#include "ex_declation.h"
#include "class_one.h" 


The thing is, if I comment out the class declarations in the ex_declaration.h and .cpp

the programm compiles with no errors and with the extern declaration of the int a = 7;,
but if I want decalir an object of my class as extern the compiler throws these errors.
Last edited on
A couple problems with your #includes:

1) You have circular includes. stdafh.h includes ex_declaration.h which also includes stdafx.h. Yes, ex_declaraion.h is protected by #pragma once , but stdafx.h does not appear to be. In any case, having circular includes is a bad practice.

2) In stdafx.h, the include for ex_declaration.h precedes the include for class_one.h. Since ex_declaration.h references class_one, the include for class_one.h should precede the include for ex_declaration.h.


2) In stdafx.h, the include for ex_declaration.h precedes the include for class_one.h. Since ex_declaration.h references class_one, the include for class_one.h should precede the include for ex_declaration.h.


That's the solution, thx very much.
I ever thought that it doesn't matter in which sequence the header files are included.
If you need to worry about the order of header file inclusion you're doing something wrong.
@Jensdvvgsr

Can I ask why do you want a global instance of your class? :+)

There is a Design Pattern called Singleton, even then there are lots of folks against the use of them as well.

And why you need a pointer to it? There shouldn't be a need for a naked pointer in modern C++ especially one created with new. Consider using a smart pointer, if at all. There is a lot that can be achieved with the STL.

Maybe if we knew what your actual problem involves, we could help with the design of it ? :+)

Regards :+)
Topic archived. No new replies allowed.