Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
/*Header.h file*/
#include <iostream>
using namespace std;
int main(){
/*Menu*/
/*case1:
int x;
cout << "Input ammount:";
cin >> x
sugarcake(x)*/
/*case2:
something else
*/
}
/*sugarcake.cpp file*/
#include <iostream>
#include "Header.h"
using namespace std;
void sugarcake(int x) {
cout << (x * 0.75) << "st egg" << endl;
cout << (x * 0.75) << "dl sugar" << endl;
cout << (x * 0.5) << "tsk vanillasugar" << endl;
cout << (x * 0.5) << "tsk blabla" << endl;
cout << (x * 0.75) << "dl wheatflour" << endl;
cout << (x * 18.75) << "gram butter" << endl;
cout << (x * 0.25) << "dl water" << endl;
}
How do i make this work or have i understood it completely wrong? (Started out with C++ yesterday so please be kind and i haven't been able to find any clear answer that works for me elsewhere)
TL DR: Call the function sugarcake(int x) in sugarcake.cpp inside the Header.h
You should not put code in the header file, instead use two source (.cpp) files, and let the header file only contain a declaration of (in your case) the sugarcake function.
So the header file should look something like
// These first two line (and the last line of the file) is part of
// a header include guard (see https://en.wikipedia.org/wiki/Include_guard)
#ifndef HEADER_H
#define HEADER_H
void sugarcake(int x);
#endif // HEADER_H
The sugarcake.cpp file:
#include <iostream>
#include "header.h" // Technically we don't need to include the header
// file here, but it's always a good idea to do
// anyway
void sugarcake(int x) {
...
}
And finally the main.cpp file:
#include <iostream>
#include "header.h"
int main() {
int x;
std::cin >> x;
sugarcake(x);
}
How to build this depends on your environment, but if you're using GCC from the command line then you do like this
$ g++ -Wall -Wextra main.cpp sugarcake.cpp -o sugarcake
The -Wall and -Wextra options are to enable extra warnings, always a good idea. The -o options tells g++ what to name the executable program it creates. The two source files are the files you just created.
We use header files to describe functions which will be used after in another occasion per say.
So I will show how I organize my functions in the header files:
/* header.h
* include guards below, it is very import to use it to be sure you'r not
* including things twice.
*/
#ifndef HEADER_H
#define HEADER_H
// I write function prototypes in the header files.
int sum(int &a, int &b);
#endif // HEADER_H
The definition of the function is written in the header.cpp
// header.cpp
#include "header.h"
int sum(int &a, int &b)
{
int total = a + b;
return total;
}
To use that function in main.cpp you just have to include the header.cpp
// main.cpp
#include <iostream>
#include "header.h"
int main(int argc, char *argv[])
{
int a = 5;
int b = 4;
int total = sum(a, b);
std::cout << "The total sum of the 4 and 5 numbers is: " << total << std::endl;
}
See how it is made? Then you can compile the cpp files and it will work!
Related
This question already has answers here:
linkage error:multiple definitions of global variables
(1 answer)
Multiple definition error on variable that is declared and defined in header file and used only in its cpp file
(3 answers)
Closed 2 years ago.
I have recently started learning c++ after learning a good amount of Javascript.
I'm pretty sure that the problem I am facing has to do with scope/how many times the header file is #included, but I need some clarification.
If you look at what I have - you can see that I have "int input" commented out in input.hpp, and I have it actually declared in input.cpp within the scope of the getInput() function. Can someone explain in detail why I am able to compile the way it is written here, but it won't compile if I switch where "int input" is declared? NOTE: The error I get is " multiple definition of `input' "
Also, I know this is a long post so if I need to ask this in another post please tell me. Since I am new to C++, have I separated my files correctly?
These are my files:
main.cpp
#include <iostream>
#include "add.hpp"
#include "input.hpp"
int x = getInput();
int y = getInput();
int main()
{
std::cout << add(x, y) << std::endl;
return 0;
}
add.cpp
#include "add.hpp"
#include <iostream>
int addCalc (int x, int y)
{
return (x + y);
}
std::string add (int x, int y)
{
return (std::to_string (x) + " + " + std::to_string (y) + " = " + std::to_string (addCalc (x, y)));
}
add.hpp
#ifndef add_hpp
#define add_hpp
#include <iostream>
int addCalc(int x, int y);
std::string add(int x, int y);
#endif
input.cpp
#include "input.hpp"
#include <iostream>
int getInput()
{
int input;
std::cout << "Enter a number: " << std::endl;
std::cin >> input;
return input;
}
input.hpp
#ifndef input_hpp
#define input_hpp
#include <iostream>
int getInput();
//int input;
#endif
Any and all comments would be appreciated.
Thanks in advance!
This question already has answers here:
When to use extern in C++
(4 answers)
Closed 3 years ago.
I've got aud.ccp, aud.h, geist.ccp, geist.h. In geist.ccp I've got a variable which needs to get to aud.ccp.
If I got:
int x = 5;
in geist.ccp, how can I achieve it, that a 8 gets represented in the console when I use
cout << x+y << endl;
as well as
cin >> y; // ofc I enter 3 here.
in aud.ccp.
Edit:
I wrote:
int x
in the public part of geist.h
and I wrote:
x = 5;
in geist.cpp.
Finaly I wrote
extern int x;
in aud.cpp
But somehow I do not get the result I want
You need to declare the variable in a public scope of one module:
int x;
and declare its use in another one:
extern int x;
Then both modules, when linked together, will use the same variable.
It's most conveniently done with the defining declaration (with an optional initializer) placed in a .cpp module, and the extern declaration put into a .h file. Then each module, both the one defining the variable and those importing it, see the same extern declaration, which guarantees the declaration is same as an actual definition of the variable.
You have to care about "redefinition x variable Error" in your code.
You can Try this method:
geist.h:
#ifndef GEIST_H
#define GEIST_H
int x {5};
#endif
geist.cpp:
#include "geist.h"
#include <iostream>
using namespace std;
void printname()
{
cout << "The X value is" << x <<"\n";
}
aud.h:
#ifndef AUD_H
#define AUD_H
extern int x;
void Add_X_with_User_Desire();
#endif
aud.cpp:
#include "aud.h"
#include <iostream>
using namespace std;
void Add_X_with_User_Desire()
{
int y{0};
cout << "Please Enter an Integer Number: "<< "\n";
cin >> y;
cout << "y + x: " << x+y<<"\n";
}
and finally main function:
stack59228825.cpp:
#include <iostream>
#include "aud.h"
int main()
{
std::cout <<"X variable in main function is:" <<x << "\n";
Add_X_with_User_Desire();
x = 10;
std::cout << "targetVariable in main function is:" << 10 << "\n";
Add_X_with_User_Desire();
}
I have 2 files, main.cpp and xyz.cpp, xyz.cppp have function that making some calculation (and should to output it at the end), and i want call this function from switch in main.cpp
main.cpp :
#include <iostream>
#include <math.h>
#include <cstdlib>
#include "xyz.cpp"
int cl;
using namespace std;
int main(int argc, const char * argv[]){
cout << ("Make ur choice (1-1)");
cin >> cl;
switch(cl){
case (1):{
// I suppose it should be called here somehow
}
}
return 0;
}
xyz.cpp:
using namespace std;
int function() {
cout << "Input number: ";
cin >> a;
o1p1 = (1+cos(4*a));
o1p2 = (1+cos(2*a));
o1 = ((sin(4*a))/o1p1)*((cos(2*a))/o1p2);
cout << "\nZ1 = ";
cout << o1;
cout << "\n ";
return 0;
}
Where you have your comment, simply write:
function();
However, note that typically you will want to include header files (i.e. a file with function declarations), rather than source files (a file with the definitions).
In the header you will have:
int function();
The source file would be the same.
Note that this will mean that you will have to compile both source files, rather than just the main one.
Rename your method, otherwise the call is going to be ambiguous.
Use a header file named "xyz.h", where you declare your method. Then, in the main.cpp file, include that header file (instead of its source file). The source file "xyz.cpp" should include the header file as well. Then in main.cpp, just call the method like this: int returnedValue = myFunction();
Complete example:
xyz.h
#ifndef XYZ_H
#define XYZ_H
int myFunction();
#endif /* XYZ_H */
xyz.cpp
#include <iostream>
#include <cmath>
#include "xyz.h"
using namespace std;
int myFunction() {
float a, o1p1, o1p2, o1;
cout << "Input number: ";
cin >> a;
o1p1 = (1+cos(4*a));
o1p2 = (1+cos(2*a));
o1 = ((sin(4*a))/o1p1)*((cos(2*a))/o1p2);
cout << "\nZ1 = ";
cout << o1;
cout << "\n ";
return 0;
}
main.cpp
#include <iostream>
#include "xyz.h"
using namespace std;
int main(int argc, const char * argv[]) {
int cl;
cout << ("Make ur choice (1-1)");
cin >> cl;
switch(cl){
case (1):{
int returnedValue = myFunction();
cout << returnedValue << endl;
}
}
return 0;
}
Output:
Georgioss-MBP:Desktop gsamaras$ g++ main.cpp xyz.cpp -lm
Georgioss-MBP:Desktop gsamaras$ ./a.out
Make ur choice (1-1)1
Input number: 2
Z1 = -2.18504
0
I'm learning C++ and tutorial asks me to add another project to what I have now.
Also I'm asked to use forward declaration so I can make use of that added file.
Here is my main project:
#include <iostream>
#include "io.cpp"
using namespace std;
int readNumber();
void writeResult(int x);
int main() {
int x = readNumber();
int y = readNumber();
writeResult(x + y);
return 0;
}
here's the added file called io.cpp:
#include <iostream>
using namespace std;
int readNumber() {
cout << "Enter a number: ";
int x;
cin >> x;
return x;
}
void writeResult(int x) {
cout << "Sum of your numbers is " << x << endl;
}
![And here's a screenshot so you can see what error I'm getting which talks about multiple definition and you can see where those two files are added.
According to the tutorial my code is okay but compiler complains. Why ?]1
In codeblocks, when creating a new class, it should automatically header file. Programming with header files is the best practice out there. Here's the code I tried and it worked, with io.h.
main.cpp
#include <iostream>
#include "io.h"
using namespace std;
io inOut;
int main()
{
int x = inOut.readNumber();
int y = inOut.readNumber();
inOut.writeResult(x + y);
return 0;
}
io.h
#ifndef IO_H
#define IO_H
class io
{
public:
int readNumber();
void writeResult(int);
};
#endif
io.cpp
#include <iostream>
#include "io.h"
using namespace std;
int io::readNumber()
{
cout << "Enter a number: ";
int x;
cin >> x;
return x;
}
void io::writeResult(int x)
{
cout << "Sum of your numbers is " << x << endl;
}
I used codeblocks to compile the code written above, and it worked perfectly.
Well as turns out when adding more cpps they're not supposed to be #included on the top. That's what makes compiler say that function is being defined multiple times. All I had to do was just get rid off that one line.
Here's my source:
http://www.cplusplus.com/forum/beginner/44651/
I have copied this out of a book. I'm just not sure what to add in the main.cpp source file to make it run though.
I know that class declarations go in the .h file and implementations go in the .cpp file. What would I need to write in main.cpp?
I've tried lots of different things but I'm just getting so many error messages.
// cat.h
#ifndef ____2_cat_implementation__Cat__
#define ____2_cat_implementation__Cat__
#include <iostream>
using namespace std;
class Cat
{
public:
Cat (int initialAge);
~Cat();
int GetAge() { return itsAge;}
void SetAge (int age) { itsAge = age;}
void Meow() { cout << "Meow.\n";}
private: int itsAge;
};
#endif /* defined(____2_cat_implementation__Cat__) */
...
// cat.cpp
#include <iostream>
#include "Cat.h"
using namespace std;
Cat::Cat(int initialAge)
{
itsAge = initialAge;
}
Cat::~Cat()
{
}
int main()
{
Cat Frisky(5);
Frisky.Meow();
cout << "Frisky is a cat who is ";
cout << Frisky.GetAge() << " years old.\n";
Frisky.Meow();
Frisky.SetAge(7);
cout << "Now Frisky is " ;
cout << Frisky.GetAge() << " years old.\n";
return 0;
}
Look at this part again:
Cat::Cat(int initialAge);
{
itsAge = initialAge;
Cat::~Cat()
You're missing the closing } for the constructor, as well as an extra ; after the function header.
On an unrelated note, don't use global names starting with underscore (like ____2_cat_implementation__Cat__), those names a reserved by the specification.
You have a missing } and unneccessary ;
//----------------------v
Cat::Cat(int initialAge);
{
itsAge = initialAge;
}
//^
What would I need to write in main.cpp
Usially, as you have pointed out, the .h file contains the declarations and the .cpp file - the definitions. Then, the main.cpp file should contain the main function (it's not necessary to name the file, containing the main function main.cpp. It could be anything.
So, in your example, you can create a main.cpp file with the following content:
// include the declarations file
#include "cat.h"
// include the header for cin/cout/etc
#include <iostream>
using namespace std;
int main()
{
Cat Frisky(5);
Frisky.Meow();
cout << "Frisky is a cat who is ";
cout << Frisky.GetAge() << " years old.\n";
Frisky.Meow();
Frisky.SetAge(7);
cout << "Now Frisky is " ;
cout << Frisky.GetAge() << " years old.\n";
return 0;
}
Other notes:
using namespace std; is bad practice, especially in header files. Use std:: instead (for example, std::cout, std::cin, std::string, etc)
as you have .h and .cpp files, don't put half of the implementation in the header and the rest - in the source file. Put all definitions inside the source files (unless you want to inline the functions, implemented in the header)
avoid using names, starting with __ or _ - they are reserved by the standard.