extern comand and cpp files

what i am missing;
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
using namespace std;

string name;

void main () 
{
	
	s();
	cout<<name;
}


add.cpp
1
2
3
4
5
6
extern string name;

void  s() {

	name="jasmin";
}


Five errors;
Which errors ?
Are you #including <string> in add.cpp ?
I'm just a beginner but, shouldn't the other file be somehow included? Function s() is defined in other file as well. Sorry if I'm wrong, It's just my PHP heritage. :)
Last edited on
Next time, instead of saying how many errors you got, say what those errors were.

shouldn't the other file be somehow included?
No.

Function s() is defined in other file as well.
That will produce a linker error if the other function also takes the same parameters.
I am sorry, here is error list:

Error 2 error C2146: syntax error : missing ';' before identifier 'name' c:\documents and setting.........\add.cpp 3

Error 5 error C2440: '=' : cannot convert from 'const char [7]' to 'int' c:\documents and setting....\add.cpp 7

Error 1 error C3861: 's': identifier not found c:\documents and settin...\main.cpp 11

Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and setting....\add.cpp 3

Error 4 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings...\add.cpp 3

And also when I write function declaration for s(); in main.cpp Error number 1 disapear,
so here is main.cpp

1
2
3
4
5
6
7
8
string name;
void s ();
void main () 
{
	
	s();
	cout<<name;
}


and also when I include <string> in add.cpp get those four errors.
Last edited on
1st error: I can't figure this one out. Did you modify the code since your first post?
2nd error: See 4.
3rd error: ---
4th error: You removed extern string name; from add.cpp, didn't you? Now the compiler has no idea what name is supposed to be. Since it cannot assume that it's any type, it fails.
5th error: No idea why there are two of these.
extern string name; is in add.cpp, I didnt remove it.
----------------------------------------------------------------------------------
But can someone write to me code how can i do this without errors,
you know what i want from the abowe example;
in add.cpp to modify string in main.cpp.(I realy need this)
Thanks.

In above example if I instend of string put char, int... it works, but with string don't.

Problem is in string, maybe string have specifed declaration when we want it use in external file.

Last edited on
@helios
No.

Sorry I thought that he needs to add header file and include it.
That will produce a linker error if the other function also takes the same parameters.

No I didn't thought for him to put definition in both files as well. I typed wrong I see that now, but I won't edit it. Mistake is a mistake. :)
Misunderstanding happened because I didn't write well. :(

Here maybe like this:
JasminMProgram.cpp
1
2
3
4
5
6
7
8
9
#include<string>
#include<iostream>
#include"funkcija.h"
std::string name;
void s();
int main(){
	s();
	return 0;
}

funkcija.h:
1
2
3
4
5
6
7
8
#ifndef FUNKCIJA_H_
#define FUNKCIJA_H_
void s(){
	extern std::string name;
	name = "Jasmin";
	std::cout<<name<<std::endl;
}
#endif /* FUNKCIJA_H_ */ 
Last edited on
arnes99: Sorry, I thought you were the OP when I wrote that. My second comment is completely wrong because I misunderstood his code.

In above example if I instend of string put char, int... it works, but with string don't.
Did you #include <string> in both files?
I'm spamming here but, I have to say that I don't know what the hell is needed here anymore. :) I'm totally confused... xD
What he wants and who's commenting what?!?
I sloved this finaly, problem is in that i didnt write in the add.cpp
1
2
#include <iostream>
using namespace std;
:):(

it can work without including <string> in add.cpp

Thanks you all fro helping me.


iostream includes string.
Topic archived. No new replies allowed.