understanding headers

I understand the concept of a header file. Store your basic definitions in one or more files relevant to your source file(s) to make compiling quicker and bug finding easier. Now my major problem is that ive checked numerous websites including here and ive yet to find a solid answer on how to implement them. The program is a rough idea of my trying to use class structures in a header file for use in a larger project. The main file is supposed to define the objects of the class and output to the screen.

.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <cstdlib>
#include <iostream>
#include <string>

#include "theheader.h"

using namespace std;

int main(){
  Cbottle::Cbottle coke(4, 5, "wavy");
  Cbottle::Cbottle milk(3, 2, "smooth");
  cout << "The height of the coke bottle is " << coke.height
  << "the weight of the coke bottle is " << coke.weight
  << "the texture of the coke bottle is " << coke.texture;
}


.h

1
2
3
4
5
6
7
class Cbottle{
  public:
    int weight;
    int height;
    string texture;
    Cbottle (int, int, string);    
};


everything written here is gleaned from numerous sites. Im not sure i am even using the proper syntax for any of this.

Any help on explaining the use of headers and or fixing my mistakes would be appreciated.
Header files are largely for declaration of types, functions, etc.

cpp files are for implementation of member functions, functions, etc.

When you get to template classes/functions and inlining, you find that the above definitions blur a bit because some implementation needs to be in header files, but for now the above will suffice.

Your syntax in the header is correct, however your header file has some issues:

1) Use #include guards.
2) #include <string> in your header to pick up the string type.
3) When you reference string in the header, you need to specify
the namespace: std::string instead of string.

Your cpp file has syntax errors. To instantiate Cbottle, it is:

Cbottle coke( 4, 5, "wavy" );

okay well i did some work on it but now I com up with a single compiler error that reads - [linker error] undefined reference to Cbottle::Cbottle(int, int, std::string)

.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdlib>
#include <iostream>
#include <string>

#include "theheader.h"

using namespace std;

int main(){
  Cbottle coke(4, 5, "wavy");
  cout << "The height of the coke bottle is " << coke.height
  << "the weight of the coke bottle is " << coke.weight
  << "the texture of the coke bottle is " << coke.texture;

  system("pause");
}


.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef MY_HEADER_H
#define MY_HEADER_H

#include <string>
using namespace std;

class Cbottle{
  public:
    int weight;
    int height;
    std::string texture;
    Cbottle(int weight, int height, std::string texture);    
};

#endif 
You need to add the implementation for Cbottle constructor, create another cpp file with that and link your main file with it
Also be sure you are linking theheader.o with main.o.

As a matter of good programming practice, never put
"using namespace" clauses in header files. (You have
one in your header file).

Thanks a lot all! Trying to study before attending college later this year for Computer Sci. Much closer to understanding it all now. :)
Topic archived. No new replies allowed.