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() 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.
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
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
#include "big_nat.h"
#include <iostream>
#include <string>
usingnamespace 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?
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>
usingnamespace 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;
}
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.
#include "header.h"
#include <iostream>
#include <string>
usingnamespace 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
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...