Need help with compiling program with three files.

So far I have three divided files. One header file, a cpp file and a main file. The problem is that when I run this program in one file it works out perfectly. Once I divide it into those three files, all hell brakes loose and it shows a bunch of garbage.
Here is what I have,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Cake.h
#pragma once
#include <iostream>
#include <string>
using namespace std;

class Cake{
private:
string Flavor;
double Cost;
double PricePerPound;
double Weight;
public:
Cake();
Cake(string flavor, double weight);
void setFlavor(string f);
void setCost();
void setWeight(double w);
string getFlavor();
double getCost();
double getWeight();
double getPricePerPound();
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//Cake.cpp
#include "Cake.h"
#include <iostream>
#include <string>
using namespace std;

void Cake::setFlavor(string f){Flavor = f;}
void Cake::setCost(){
Cost = PricePerPound * Weight;
}
void Cake::setWeight(double w){Weight = w;}
string Cake::getFlavor(){return Flavor;}
double Cake::getCost(){return Cost;}
double Cake::getWeight(){return Weight;}
double Cake::getPricePerPound(){return PricePerPound;}

Cake::Cake(){
Flavor = "Vanilla";
PricePerPound = 10;
Weight = 0;
Cost = 0;
}

Cake::Cake(string cakeFlavor, double cakeWeight){
Flavor = cakeFlavor;
Weight = cakeWeight;
PricePerPound = 10;
setCost();
}

void validateInput(Cake custom){
if(custom.getFlavor() == "Vanilla" || custom.getFlavor() == "Chocolate" || custom.getFlavor() == "Lemon"){
cout << "The cake flavor has been recorded." << endl;
}
else{
cout << "(Invalid Input) Choose from one of the possible flavors.\n";
}
if(custom.getWeight() >= 1 && custom.getWeight() <= 10){
cout << "The cake weight has been recorded." << endl;
}
else{
cout << "(Invalid Input) Choose the weight of the cake that lies between 1-10 pounds.\n";
}
}

void changeInput(Cake &mycake, string &flavor_input, double &weight_input){
string option;
cout << "Would you like to make any changes to your customized cake? (Please enter either Yes or No): ";
cin >> option;
if(option == "Yes"){
cout << "Enter the flavor of the cake that you want: ";
cin >> flavor_input;
cout << "Enter the weight of the cake that you want (in pounds): ";
cin >> weight_input;
if(flavor_input == "Vanilla" || flavor_input == "Chocolate" || flavor_input == "Lemon"){
if(weight_input >= 1 && weight_input <= 10){
cout << "The changes of your cake has been made." << endl;
}
}
else{
cout << "(Invalid Input) Please choose possible features.\n";
}
if(option == "No"){
cout << "No changes has been made to your cake.\n";
}
mycake.setFlavor(flavor_input);
mycake.setWeight(weight_input);
mycake.setCost();
}
}

void displayInput(Cake output){
cout << "Flavor: " << output.getFlavor() << endl;
cout << "Cost: $" << output.getCost() << endl;
cout << "Price Per Pound: $" << output.getPricePerPound() << endl;
cout << "Weight: " << output.getWeight() << "lbs" << endl;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//main.cpp
#include "Cake.h"
#include "Cake.cpp"
#include <iostream>
#include <string>
using namespace std;

int main(){
Cake cake;
string flavor_input;
double weight_input;
cout << "Here are the cake flavor options: Vanilla, Chocolate, and Lemon." << endl;
cout << "Enter the flavor of the cake that you want: ";
cin >> flavor_input;
cout << "Enter the weight of the cake that you want (in pounds): ";
cin >> weight_input;
Cake mycake(flavor_input, weight_input);
validateInput(mycake);
changeInput(mycake, flavor_input, weight_input);
displayInput(mycake);
return 0;
}


Any help is appreciated!
Last edited on
Don't include the .cpp file. In general, you should only include header files.
When you compile it, mention both cpp files on the compile line. They will both be compiled into object files and the object files will be linked into the executable (and then the object files will be deleted in this case).
Any help is appreciated!

and ignored?

(You already had a thread about code in multiple files: http://www.cplusplus.com/forum/beginner/245107/ )
Hello Awsom3Alan3,

tpb wrote:
Don't include the .cpp file. In general, you should only include header files.

I believe he was referring to "main" here. ".cpp" files should be kept separate, compiled separate and then linked together to form the ".exe" file. The Ide should take care of this for you. If you are working from the command line you could use a "make file" or you will have to use the command line to tell the compiler what files need to be compiled and then what files need to be linked.

In your header files include files like "iostream" and "string" these should be in the ".cpp" files not header files. And the line using namespace std; should never be in a header file. This is worth reading: http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

Enoizat wrote:,
You also wrote using namespace std; which introduces all the standard library names in the global namespace: IMHO, that means you’re doing your best to shoot yourself in the foot.

Last point. There are times where the order of include files does make a difference.
1
2
3
4
#include <iostream>
#include <string>

#include "Cake.h" 

Because "iostream" and "string" come first anything in "Cake.h" that would need these header files will be covered.

The way you have it "Cake.h" comes first, but needs the "string" header file to cover the two "std::strings". Since the order in the ".cpp" file is backwards the ".h" file does not know about "string" yet and that is why you think you need to include "string" in the header file.

Hope that helps,

Andy
Last point. There are times where the order of include files does make a difference.

Poor style, if it does.
Since the order in the ".cpp" file is backwards

A piece of code should be self-sufficient. Why should I need to know that I have to include header X, before I'm allowed to include your header Y?


Do we need definition of Bar here?
1
2
3
struct Foo {
  yeah( Bar );
}

No. It is enough to know what the identifier Bar is:
1
2
3
4
5
class Bar; // forward declaration

struct Foo {
  yeah( Bar );
}

That we can include as is and alone.

If we want to call yeah(), then we need a Bar object and for that we need the definition.

Can we then?
1
2
3
4
5
6
7
namespace std {
  class string; // forward declaration?
}

struct Foo {
  yeah( string );
}

No. The std::string is not that trivial and we are not allowed to insert anything into the namespace std anyway.

Hence:
1
2
3
4
5
6
7
8
#ifndef FOO_H
#define FOO_H
#include <string>

struct Foo {
  yeah( std::string );
}
#endif 

Should we include the <iostream> too? Definitely not.
Nothing in that header requires anything from iostream.

How about:
1
2
3
4
struct Foo;
// do we need iostream here?

std::ostream& operator<< ( std::ostream&, const Foo& );

std::ostream is no simpler than the std::string, but there is:
1
2
3
4
#include <iosfwd> // forward declares the ostream
struct Foo;

std::ostream& operator<< ( std::ostream&, const Foo& );
I see now,
Thanks for the input guys!
Topic archived. No new replies allowed.