Jump to content

Structuring C++

Go to solution Solved by Ciccioo,

the idea is that you should have your declarations in a header file (.h), and the implementations of the declared things in an implementation file (.cpp)
 
i'll slightly modify your code to make it more how it's supposed to be, so let's start from this code:

#include <iostream>using namespace std;int calculation (int x, int y, char operation){        int c;	switch (operation)	{		case '+':			c = x + y;			break;		case '-':			c = x - y;			break;		case '*':			c = x * y;			break;		case '/':			c = x / y;			break;	}        return c;}int main(){    int a, b, c;    char operation;    cout << "Enter Calculation: ";    cin >> a >> operation >> b;    c = calculation(a,b,operation);    cout << c;    return 0;}

 
now you can split into
int calculation(int x, int y, char operation);
#include "calculations.h" // i need the declaration of what i'm about to implementint calculation (int x, int y, char operation){        int c;	switch (operation)	{		case '+':			c = x + y;			break;		case '-':			c = x - y;			break;		case '*':			c = x * y;			break;		case '/':			c = x / y;			break;	}        return c;}


 

finally, the file that uses the function, main.cpp

#include <iostream>#include "calculations.h" // with this, i bring into this file all the functionalities that are declared into calculations.h, without needing to know their implementationsusing namespace std;int main(){    int a, b, c;    char operation;    cout << "Enter Calculation: ";    cin >> a >> operation >> b;    c = calculation(a,b,operation);    cout << c;    return 0;}

 

you pretty much want to keep definitions seperate from implementations

your code didn't compile because the variable a, b, c, and operation don't exist in the other files. there are ways to make variables visible across files, but in this case (and very very very often) it's a better idea to stay away from global variables and keep them local to the function, using parameters to reach data from around the program

So I've spent some time learning C++ and managed to make a fairly simple calculator thus far. One thing that's really confusing me though is how to structure projects. I'm coming from a beginner Java background so there are many things that confuse me so please be patient.

 

I've done some reading and I've come across this concept of headers and source files. What's the point of these? From what I can gather the main point is the headers declare stuff and the rest of the code is in the source, but why not just include the declarations in the source?

 

I've attached my code below.

 

#include <iostream>using namespace std;int a, b, c;char operation;void calculation (int x, int y){	switch (operation)	{		case '+':			c = x + y;			break;		case '-':			c = x - y;			break;		case '*':			c = x * y;			break;		case '/':			c = x / y;			break;	}}int main(){    cout << "Enter Calculation: ";    cin >> a >> operation >> b;    calculation(a,b);    cout << c;    return 0;}

I'm presuming it's possible to move the calculation function to an external file - how do you go about that?

 

I tried basically copying the function to another file and adding to the main:

#include "calculation.cpp"
But it seems to bitch about the variable c. Why is that?

All help is appreciated. :)

Link to comment
https://linustechtips.com/topic/292888-structuring-c/
Share on other sites

Link to post
Share on other sites

As you describe it, headers are generally for declaring classes, including their methods and fields, while cpp files are generally for defining the methods, and also for main().  However, virtual methods (not related to your question but I felt like I should mention it) need to be defined inside the header file, but outside the class definition, so that they are available at compile time (vs linker time if in a separate .cpp file).

 

In your case where, calculation seems more like a helper function, its fine to have it inside the same file as main().  However, if you had a class like Calculator, you could move that declaration into a .h file, and its method definitions into a .cpp file.

 

Also, its unnecessary to #include .cpp files.  

2500k | Z68-UD3H-B3 | GTX 570 Classified + Accelero Xtreme III | MX 200 250GB + Seagate 500GB | R4 | Seasonic 660W Plattinum | 8GB G Skill | Acer K272HUL + 2 x Dell P2417H, Rosewill dual mount, HP 22bw | Mackie CR3, ATH-M50X | DAS Keyboard | Mobile: X1 Carbon, Nexus 6P, LG G3, Samsung S5, Nexus 7 (2013)

Link to comment
https://linustechtips.com/topic/292888-structuring-c/#findComment-3977280
Share on other sites

Link to post
Share on other sites

the idea is that you should have your declarations in a header file (.h), and the implementations of the declared things in an implementation file (.cpp)
 
i'll slightly modify your code to make it more how it's supposed to be, so let's start from this code:

#include <iostream>using namespace std;int calculation (int x, int y, char operation){        int c;	switch (operation)	{		case '+':			c = x + y;			break;		case '-':			c = x - y;			break;		case '*':			c = x * y;			break;		case '/':			c = x / y;			break;	}        return c;}int main(){    int a, b, c;    char operation;    cout << "Enter Calculation: ";    cin >> a >> operation >> b;    c = calculation(a,b,operation);    cout << c;    return 0;}

 
now you can split into
int calculation(int x, int y, char operation);
#include "calculations.h" // i need the declaration of what i'm about to implementint calculation (int x, int y, char operation){        int c;	switch (operation)	{		case '+':			c = x + y;			break;		case '-':			c = x - y;			break;		case '*':			c = x * y;			break;		case '/':			c = x / y;			break;	}        return c;}


 

finally, the file that uses the function, main.cpp

#include <iostream>#include "calculations.h" // with this, i bring into this file all the functionalities that are declared into calculations.h, without needing to know their implementationsusing namespace std;int main(){    int a, b, c;    char operation;    cout << "Enter Calculation: ";    cin >> a >> operation >> b;    c = calculation(a,b,operation);    cout << c;    return 0;}

 

you pretty much want to keep definitions seperate from implementations

your code didn't compile because the variable a, b, c, and operation don't exist in the other files. there are ways to make variables visible across files, but in this case (and very very very often) it's a better idea to stay away from global variables and keep them local to the function, using parameters to reach data from around the program

Link to comment
https://linustechtips.com/topic/292888-structuring-c/#findComment-3977326
Share on other sites

Link to post
Share on other sites

the idea is that you should have your declarations in a header file (.h), and the implementations of the declared things in an implementation file (.cpp)

 

i'll slightly modify your code to make it more how it's supposed to be, so let's start from this code:

#include <iostream>using namespace std;int calculation (int x, int y, char operation){        int c;	switch (operation)	{		case '+':			c = x + y;			break;		case '-':			c = x - y;			break;		case '*':			c = x * y;			break;		case '/':			c = x / y;			break;	}        return c;}int main(){    int a, b, c;    char operation;    cout << "Enter Calculation: ";    cin >> a >> operation >> b;    c = calculation(a,b,operation);    cout << c;    return 0;}

 

now you can split into

 

finally, the file that uses the function, main.cpp

#include <iostream>#include "calculations.h" // with this, i bring into this file all the functionalities that are declared into calculations.h, without needing to know their implementationsusing namespace std;int main(){    int a, b, c;    char operation;    cout << "Enter Calculation: ";    cin >> a >> operation >> b;    c = calculation(a,b,operation);    cout << c;    return 0;}

 

you pretty much want to keep definitions seperate from implementations

your code didn't compile because the variable a, b, c, and operation don't exist in the other files. there are ways to make variables visible across files, but in this case (and very very very often) it's a better idea to stay away from global variables and keep them local to the function, using parameters to reach data from around the program

 

Tyvm. This answered all my questions.

int calculation(int x, int y, char operation);
#include "calculations.h" // i need the declaration of what i'm about to implementint calculation (int x, int y, char operation){        int c;	switch (operation)	{		case '+':			c = x + y;			break;		case '-':			c = x - y;			break;		case '*':			c = x * y;			break;		case '/':			c = x / y;			break;	}        return c;}
Link to comment
https://linustechtips.com/topic/292888-structuring-c/#findComment-3977448
Share on other sites

Link to post
Share on other sites

the idea is that you should have your declarations in a header file (.h), and the implementations of the declared things in an implementation file (.cpp)

 

i'll slightly modify your code to make it more how it's supposed to be, so let's start from this code:

#include <iostream>using namespace std;int calculation (int x, int y, char operation){        int c;	switch (operation)	{		case '+':			c = x + y;			break;		case '-':			c = x - y;			break;		case '*':			c = x * y;			break;		case '/':			c = x / y;			break;	}        return c;}int main(){    int a, b, c;    char operation;    cout << "Enter Calculation: ";    cin >> a >> operation >> b;    c = calculation(a,b,operation);    cout << c;    return 0;}

 

now you can split into

 

finally, the file that uses the function, main.cpp

#include <iostream>#include "calculations.h" // with this, i bring into this file all the functionalities that are declared into calculations.h, without needing to know their implementationsusing namespace std;int main(){    int a, b, c;    char operation;    cout << "Enter Calculation: ";    cin >> a >> operation >> b;    c = calculation(a,b,operation);    cout << c;    return 0;}

 

you pretty much want to keep definitions seperate from implementations

your code didn't compile because the variable a, b, c, and operation don't exist in the other files. there are ways to make variables visible across files, but in this case (and very very very often) it's a better idea to stay away from global variables and keep them local to the function, using parameters to reach data from around the program

 

 

Tyvm. This answered all my questions.

For some reason I get an error when building? "undefined reference to calculation(int,int,char)". Any ideas? I don't get when I change the #include calculation.h to .cpp in main.cpp.

int calculation(int x, int y, char operation);
#include "calculations.h" // i need the declaration of what i'm about to implementint calculation (int x, int y, char operation){        int c;	switch (operation)	{		case '+':			c = x + y;			break;		case '-':			c = x - y;			break;		case '*':			c = x * y;			break;		case '/':			c = x / y;			break;	}        return c;}
Link to comment
https://linustechtips.com/topic/292888-structuring-c/#findComment-3977691
Share on other sites

Link to post
Share on other sites

For some reason I get an error when building? "undefined reference to calculation(int,int,char)". Any ideas? I don't get when I change the #include calculation.h to .cpp in main.cpp.

copypaste here the content of your calculation.h

 

(1000 posts yay)

Link to comment
https://linustechtips.com/topic/292888-structuring-c/#findComment-3977738
Share on other sites

Link to post
Share on other sites

int calculation (int x, int y, char operation);

Oh I should have said - the error was picking on the c = ... bit on main.cpp.

 

did you  #include calculation.h? how are you compiling the program? calculation.cpp needs to be in the same "project" or whatever you're using to compile

Link to comment
https://linustechtips.com/topic/292888-structuring-c/#findComment-3977780
Share on other sites

Link to post
Share on other sites

did you  #include calculation.h? how are you compiling the program? calculation.cpp needs to be in the same "project" or whatever you're using to compile

#include calculation.h was in both main.cpp and calculation.cpp. All 3 are in the same directory and I'm building using Code::Blocks (and they're all in the same project).

Link to comment
https://linustechtips.com/topic/292888-structuring-c/#findComment-3977818
Share on other sites

Link to post
Share on other sites

#include calculation.h was in both main.cpp and calculation.cpp. All 3 are in the same directory and I'm building using Code::Blocks (and they're all in the same project).

check the function names, check that they all return an int

if it still doesn't work, copypaste here everything

Link to comment
https://linustechtips.com/topic/292888-structuring-c/#findComment-3977837
Share on other sites

Link to post
Share on other sites

main.cpp

#include <iostream>#include "calculation.h" using namespace std; int main(){    int a, b, c;    char operation;     cout << "Enter F to exit" << endl << "Enter Calculation: ";    cin >> a >> operation >> b;    c = calculation(a,b,operation);    cout << c;    return 0;}

calculation.cpp

#include "calculation.h"int calculation (int x, int y, char operation){	int c;	switch (operation)	{		case '+':			c = x + y;			break;		case '-':			c = x - y;			break;		case '*':		case 'x':			c = x * y;			break;		case '/':			c = x / y;			break;	}        return c;}

calculation.h

int calculation (int x, int y, char operation);
Link to comment
https://linustechtips.com/topic/292888-structuring-c/#findComment-3977885
Share on other sites

Link to post
Share on other sites

main.cpp

 

#include <iostream>#include "calculation.h" using namespace std; int main(){    int a, b, c;    char operation;     cout << "Enter F to exit" << endl << "Enter Calculation: ";    cin >> a >> operation >> b;    c = calculation(a,b,operation);    cout << c;    return 0;}

calculation.cpp?

Link to comment
https://linustechtips.com/topic/292888-structuring-c/#findComment-3977894
Share on other sites

Link to post
Share on other sites

i copypasted your code in a new code::blocks project, it works

triple check that everything is in the same project

I've noticed on the Management panel on the side calculation.h is greyed? Not sure why that's happened.

EDIT: compiling that file by itself brings up the message "That file isn't assigned to any target".

Link to comment
https://linustechtips.com/topic/292888-structuring-c/#findComment-3977959
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×