header and implementation files...

Hello everyone, I need a bit of help with this program. It's a simple program I am coding to practice making header and implementation files. The program is not compiling and I have no idea how to make it work at this point. I just want the user to input a number, then I want to convert that string into an integer and output to the screen. Here is what I have so far:

big_nat.h
1
2
3
4
5
6
7
8
#include <iostream>

class Big_Nat {
	string str;
	int s_int;
public:
	Big_Nat();
}


big_nat.cpp
1
2
3
4
5
6
7
#include <iostream>
#include "big_nat.h"

Big_Nat::Big_Nat(str)
{
	s_int = atoi(str.c_str());
}



Big_Nat_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "big_nat.h"
#include <iostream>

using namespace std;

int main()
{
	string s;

	cout << "Give x : "; getline(cin,s,'\n');
	Big_Nat x(s);
	cout << x << endl;

	return 0;
}


Do I need to include iostream in each of the files? What else am I doing wrong? Any help will be appreciated. Thanks!
If you are using strings don't you need to declare so with #include <string> ?
How to create a header:


1
2
3
4
5
6
7
#ifndef HEADER-NAME-IN-CAPITALS_H
#define HEADER-NAME-IN-CAPITALS_H

//includes
//functions/classes/code

#endif 


How to implement it:

1
2
3
4
#include "header-name-in-capitals.h"

//includes
//code 


Just add the ifndef, define, and endif, and it should work.
Big_Nat() on line7 of big_nat.h takes no parameters, and then in big_nat.cpp it takes a parameter of type "str". You need to change the one in big_nat.h to be Big_Nat(std::string); and the one in big_nat.cpp to be Big_Nat::Big_Nat(std::string str)

@MottMan: Macros do not have to be in all caps, that is simply a common thing to do. It can be any case combination.
Last edited on
Thank you all so far. I have taken the advice given in the replies but my program still comes up with many errors. Maybe I am not compiling correctly. I am creating the files in Notepad(simple text editor in Windows) and compiling with g++. In the console window, I type in "g++ big_nat.cpp Big_Nat_test.cpp -o big_nat". Is there another way I need to compile this so that the files link together? Also, I am getting errors such as "'cout' was not declared in this scope." Why is this? I have declared that I am using the std namespace... Here is what I have so far:


big_nat.h
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef big_nat_h
#define big_nat_h
#include <iostream>
#include <string>

class Big_Nat {
	string str;
	int s_int;
public:
	Big_Nat(std::string);
}
#endif 



big_nat.cpp
1
2
3
4
5
6
7
8
#include <iostream>
#include "big_nat.h"
#include <string>

Big_Nat::Big_Nat(std::string str)
{
	s_int = atoi(str.c_str());
}



Big_Nat_test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "big_nat.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string s;

	cout << "Give x : "; getline(cin,s,'\n');
	Big_Nat x(s);
	cout << x << endl;

	return 0;
}

Alright, I changed some stuff around and got less errors but I am still getting the error "no match for 'operator<<' in 'std::cout << x'". Here is what I have now:
big_nat.h
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef big_nat_h
#define big_nat_h
#include <iostream>
#include <string>

class Big_Nat {
	std::string str;
	int s_int;
public:
	Big_Nat(std::string);
};
#endif 


big_nat.cpp
1
2
3
4
5
6
7
8
#include <iostream>
#include "big_nat.h"
#include <string>

Big_Nat::Big_Nat(std::string str)
{
	s_int = atoi(str.c_str());
};


Big_Nat_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "big_nat.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
	std::string s;

	std::cout << "Give x : "; getline(std::cin,s,'\n');
	Big_Nat x(s);
	std::cout << x << std::endl;

	return 0;
}


I am confused about using namespace std. I thought if I declared that, then I could use cout instead of std::cout, but it seems that if I don't use std::cout, I get many errors. Can anyone tell me what I am doing wrong here?
cout doesn't know how you want it to print out x, because x is your own class and it doesn't define any operators to be nice to cout.
thank you L B...I have figured it out with your hint. Here is what I have:

big_nat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef big_nat_h
#define big_nat_h
#include <iostream>
#include <string>

class Big_Nat {
	std::string s;
	int s_int;
public:
	Big_Nat(std::string);
	int to_int();
};
#endif 


big_nat.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "big_nat.h"
#include <string>

Big_Nat::Big_Nat(std::string s)
{
	s_int = atoi(s.c_str());
};
int Big_Nat::to_int()
{
	return s_int;	
};


Big_Nat_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "big_nat.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
	std::string s;

	std::cout << "Give x : "; getline(std::cin,s,'\n');
	Big_Nat x(s);
	std::cout << x.to_int() << std::endl;

	return 0;
}
One last thing, you never use line 7 in big_nat.h
Line 5 of big_nat.cpp. Isn't the std::string s the one I declared in line 7 of big_nat.h? I'm a bit confused now..
No when you declare parameters in functions they are copies (assuming your not using references or pointers). you don't need to declare them a second time:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
using namespace std;

void printFunction(string s) // string s does not need to be initiated anywhere. 
{ //we have done so here.

    cout << s; // string 'abcd' is sent to the function and 's' becomes a copy of 'abcd', 
    //meaning it contains the same data "Hello World"
}

int main()
{
    string abcd = "Hello World"; // we create a string called 'abcd'
    printFunction(abcd);  // we send 'abcd' to the function
    return 0;
}
Last edited on
Ah, I understand now. How about variables used in a function? Do I declare them in the private section in the header file or declare them in the implementation file?
It depends, if you need something to have global scope then in the header file is probably the best place for it, but if it's just a quick variable your only going to use in the one function then declaring in the function is fine.

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
#include "header.h"
#include <iostream>
#include <string>
using namespace std;

void h::function()
{
   int i = 10;  // i only a local var we use in this function only;
   cout << "Countdown in:" << endl;
   while ( i > 0 )
   {
      cout << i << endl;
      i--;
   }
   fileName = "HotTomatos.txt";  // this is global, (private string in header) because we are going to use it in multiple places.
   cout << "The filename we use throughout the whole program is called: " << fileName;
}

int main()
{
   h h;

   h.function();

   return 0;
}
      


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

class h
{
   public:
      void function();

   private:
      string fileName;
};

#endif 
Last edited on
Thanks gcampton, I'm glad I learned this sooner than later..I was wondering why I was getting errors saying that some of my variables weren't declared even though I thought they were...
Topic archived. No new replies allowed.