123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|608|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> Off Topic -> Undeclared

Tue, 15 Sep 2015, 07:00
spinal
if theres one thing that pisses me off more than anything in the world it's ’’function was not declared in this scope’’

-=-=-
Check out my excellent homepage!
Tue, 15 Sep 2015, 07:00
Jayenkai
People complain about my constant "globalising" of damn near everything in my code.
.. But at least I don't have to deal with crap like that!!

-=-=-
''Load, Next List!''
Tue, 15 Sep 2015, 09:08
spinal
AAAARRRRRGGGGGHHHHHHH!!!!!!!!

Someone please tell me what the hell I'm doing wrong here...

file.h


file.cpp


display_ds.i


-- Result --


Can someone please explain to me what I'm not understanding? I don't get what the problem is.

-=-=-
Check out my excellent homepage!
Tue, 15 Sep 2015, 09:31
Dabz
Have you included the file.cpp file in your build, that is, when calling g++?

Dabz

-=-=-
Intel Core i5 6400 2.7GHz, NVIDIA GeForce GTX 1070 (8GB), 8Gig DDR4 RAM, 256GB SSD, 1TB HDD, Windows 10 64bit
Tue, 15 Sep 2015, 09:37
Dabz
Another thought... Try including file.h in your file.cpp file!

Dabz

-=-=-
Intel Core i5 6400 2.7GHz, NVIDIA GeForce GTX 1070 (8GB), 8Gig DDR4 RAM, 256GB SSD, 1TB HDD, Windows 10 64bit
Tue, 15 Sep 2015, 11:57
Dabz
Mmmmmm just tried the same program, only using ints and it runs fine?

File.h

extern int var;
extern int var2;

File.cpp

int var;
int var2;

Main.cpp

#include "file.h"

int main (void)
{
var = 10;
var2 = 20;

return(0);
}


Works a charm!

Dabz

-=-=-
Intel Core i5 6400 2.7GHz, NVIDIA GeForce GTX 1070 (8GB), 8Gig DDR4 RAM, 256GB SSD, 1TB HDD, Windows 10 64bit
Tue, 15 Sep 2015, 12:42
spinal
It's things like this that make me hate c++ (and c) with every fibre of my soul.

So how would I go about stuffing the following in one file to be included where I need it?



-=-=-
Check out my excellent homepage!
Wed, 16 Sep 2015, 09:42
Dabz
I'm at a PC now, right, this works:-

Main.cpp


cfoo.h


cfoo.cpp


It builds usibng g++, on Linux, you navigate to the folder with the terminal, then (Copy and pasted from terminal):-

root@vmdebiandevbox:/home/michael/Documents/test# g++ -c main.cpp cfoo.cpp
root@vmdebiandevbox:/home/michael/Documents/test# g++ -o main main.o cfoo.o
root@vmdebiandevbox:/home/michael/Documents/test# ./main
Value: 3

Done!

One of the biggest problems people have with header files is that a) They dont use a header guard at the start, and b) a header isnt really there to initialize variables, apart from constants.

I tend to treat headers and their related cpp files as class files, ala C#, and the only way I tend to stray from that is when I put code up on the internet, as in, one file for the main program, and a header for everything else, makes it easier to read. I bloody hate it when people put up code with a million CSomething.cpp files and you end up having to file hop to unwangle how it goes, and its a sodding nightmare to build the project from the terminal as you have to compile and link all these files on the command line!

EDIT: BTW, forgot to mention... You dont need Extern in your example either

Dabz

-=-=-
Intel Core i5 6400 2.7GHz, NVIDIA GeForce GTX 1070 (8GB), 8Gig DDR4 RAM, 256GB SSD, 1TB HDD, Windows 10 64bit
Wed, 16 Sep 2015, 10:01
Dabz
Oh, and by the way, to reuse your code files, basically, just have a header folder and a lib folder somewhere on your PC, e.g.

/home/michael/myincludes
/home/michael/mylibs

Drop the cfoo.h file in the myincludes folder, and the cfoo.o file in the mylibs folder.

Then, say, if your using an IDE, you can go into the project settings, and specify said folders in the options, so, when you build your project, the IDE will automatically look in them and pull them for you. You will also need to tell the linker about the .o file, this comes under additional dependencies or something, just whack the name of it in and the IDE will stuff that file on the build call.

The bonus is, you have one place for everything... And this is another thing I hate, is when projects use a directory structure for their includes, e.g.



Crap way, so try and not get into the habit, even GTK does it #include "GTK\GTK.h", and setting up IDE's is a pain!

Dabz

-=-=-
Intel Core i5 6400 2.7GHz, NVIDIA GeForce GTX 1070 (8GB), 8Gig DDR4 RAM, 256GB SSD, 1TB HDD, Windows 10 64bit