Ich habe 4 Dateien:C++ Variable von 2 Dateien geteilt
- SHARED.H
- main.cpp
- something.h
- something.cpp
geteilt. h:
#ifndef SHARED_H
#define SHARED_H
int* sth;
#endif
something.h:
#ifndef SOMETHING_H
#define SOMETHING_H
class foo
{
public:
void printVar();
};
#endif
something.cpp:
#include <iostream>
#include "something.h"
#include "shared.h"
using namespace std;
void foo::printVar()
{
cout<<"Foo: "<<*sth<<endl;
};
main.cpp:
#include <cstdlib>
#include <iostream>
#include "shared.h"
#include "something.h"
using namespace std;
int main(int argc, char *argv[])
{
sth=new int(32);
foo x;
cout<<"Main: "<<*sth<<endl;
x.printVar();
system("PAUSE");
return EXIT_SUCCESS;
}
Compiler zurück multipe Definition * etw;
Ich habe den * sth statischen Modifier hinzugefügt und es kompiliert, aber stürzt ab. Ich habe die Aufdrucke die Adressen der Zeiger zu drucken und ich hatte Programm zurückgegeben:
Main: 0x3e0f20
Foo: 0
Warum ist der Zeiger foo nicht zugewiesen? Ich möchte es nur einmal in main zuweisen und dann in anderen Dateien teilen ... Wie kann ich das machen? Ist es etwas mit extern
Modifikator?
Danke für alle Antworten.
Sie waren zuerst, also akzeptiere ich Ihre. –