Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / How do you setup arrays to use across many cpp files?

Author
Message
HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 11th Mar 2009 15:56
Hi All,

This one got me, can do varables and arrays ok in a single cpp file but when I try to do it across many cpp with function it does not work.

Could someone show how to set up the header h file the varable cpp file and the function cpp file.
using these.

myarray[]
myvars
myfunction


cheers.

Dark Physics makes any hot drink go cold.
Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 11th Mar 2009 16:29 Edited at: 11th Mar 2009 16:29
Any variables you want to access from multiple files you should declare in a header, preceding them with the 'extern' keyword to indicate that the variable is declared in another source file, and then do a declaration without the 'extern' keyword in a source file. For example:

header.h:


source.cpp:


You then include the header in any source files you want to access the variables from.

HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 11th Mar 2009 16:39
thanks Benjamin, do I do the same when using arrays.

Dark Physics makes any hot drink go cold.
Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 11th Mar 2009 16:48
Pretty much, although you don't have to specify the subscript when doing the extern declaration.

HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 11th Mar 2009 17:07
Must be something missing.

this what I have in my vars.h file.


and this is what I have in the vars.cpp file


comes back with this error
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
and if I have const int infront of them like so


it returns the error
loadpictures.obj : error LNK2001: unresolved external symbol "int NNW_15" (?NNW_15@@3HA)

Dark Physics makes any hot drink go cold.
Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 11th Mar 2009 17:10 Edited at: 11th Mar 2009 17:16
You have to still put the data type specifier (in this case, int) before the variable in the actual source declarations. And also, both declaration and definition must match (don't apply const to one and not the other).

Oh and by the way, you don't need to do the same thing with const variables, you can just define them in a header file without having to declare them anywhere.

HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 11th Mar 2009 17:16
seem to be a lot of do it twice, in coding in c++.

Be nice when I know all the errors and how you get them, plus all the other stuff to learn.

Basic was as it say Basic.

Dark Physics makes any hot drink go cold.
HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 11th Mar 2009 17:27
well both file are set how I think they should be, however its still giving errors.

loadpictures.obj : error LNK2001: unresolved external symbol "int const NNW_15" (?NNW_15@@3HB)

Dark Physics makes any hot drink go cold.
Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 11th Mar 2009 17:37
In this case the declaration and the definition don't match.

HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 11th Mar 2009 17:38
seems have got it going by using short int myvar, why does it needed short at the start anyone?

Dark Physics makes any hot drink go cold.
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 11th Mar 2009 17:42
Quote: "seem to be a lot of do it twice, in coding in c++."


Only because of the way you're coding it. If you contained these variables in a struct or class(as you should be), you'd only need to do this for the object instance itself.

Quote: "why does it needed short at the start anyone?"


Because you made it a short? Try posting your new code.

HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 11th Mar 2009 17:52
Thanks for your replay dark coder, if you could show how to set it up as a struct or class that would help a lot.

what parts of the project need to be up load.

I see one file likes putting my computer number on the end of one the files it makes, how do I it stop doing that.

Dark Physics makes any hot drink go cold.
Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 11th Mar 2009 18:57
A short int is two bytes, it's not the same as an int which is 4.

HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 11th Mar 2009 19:07
find it strange that when I used int on it the first time round own this did not work, now I taken out all the shorts it works.

know a good link that shows how to set up a struct or class as the web site I have is thinking I know more than I do.

I get the impression that a class it very much like a UDT in DarkBasic is that right.

Dark Physics makes any hot drink go cold.
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 11th Mar 2009 19:39
Quote: "I get the impression that a class it very much like a UDT in DarkBasic is that right."


It has all of the features of a UDT yes, but it also has a billion more.

Try the tutorial HERE as well as the other ones on that site.

HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 11th Mar 2009 20:07 Edited at: 11th Mar 2009 20:08
dark coder thanks again its knowing where to look, had that web site, spent ages looking up and down it but did not see that one, as I did not connect to it begin a struc = data structure.

cheers another bit learnt.

edit

do I put these class and struc in a header file or cpp file?

Dark Physics makes any hot drink go cold.
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 11th Mar 2009 20:21
You can also use:

extern {
...
...
...
}

To declare multiple variables as extern.

[b]Yuor signutare was aresed by a deslyxic mud...
BOX2D V2 HAS HELP FILES! AND A WIKI!
HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 11th Mar 2009 20:24 Edited at: 11th Mar 2009 20:38
now thats a good one to know.

cheers Diggsey

edit
tried that it did not like it!

Dark Physics makes any hot drink go cold.
HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 11th Mar 2009 21:11
If you were to buy a book on C++ which one would you say would be a good one to get to start with.

would like one that does both, good to start with and still ok when you know it all!

Dark Physics makes any hot drink go cold.

Login to post a reply

Server time is: 2024-11-25 17:39:01
Your offset time is: 2024-11-25 17:39:01