I'm trying to make a header file called hippiewh. However, I have been running into some problems.
here's the code for the header file:
#include <iostream>
#include <string>
using namespace std;
#ifndef hippiewh_h
#define hippiewh_h
int askInt(string prompt)
{
int var_int;
cout << prompt;
cin >> var_int;
return var_int;
}
#endif
there are functions like that one for every data type. string, float, and so on.
I can't get this header file working with the online IDE and compiler at Compilr.com
The error i'm getting is:
hippiewh.h: No such file or directory
Andrew, I bet you didn't write the include like this #include "hippiewh.h".
and you did it like this #include <hippiewh.h> which is wrong for your own created header files.
If you put all the code here we could help you
It should work without hassle :/
You must include the file this way
#include "hippiewh.h"
Related
While following the book C++ For Dummies, I have three files in my CodeBlocks project, main.cpp, Pen.h, and Pen.cpp. They look like this:
main.cpp:
#include <iostream>
#include "Pen.h"
//#include "Pen.cpp"
using namespace std;
int main()
{
Pen MyPen = Pen();
MyPen.test();
}
Pen.h:
#ifndef PEN_H_INCLUDED
#define PEN_H_INCLUDED
//#include "Pen.cpp" // Uncommenting this gives a different error
using namespace std;
class Pen
{
public:
// attributes omitted
// PROTOTYPES:
// other functions omitted
void test();
};
#endif // PEN_H_INCLUDED
Pen.cpp:
#include <iostream>
#include "Pen.h"
using namespace std;
//other function definitions omitted
void Pen::test()
{
cout << "Test successful." << endl;
}
When I run the code as listed above, I get an "undefined reference to `Pen::test()'" error. To fix this, I changed the #include statements at the top of main.cpp to:
#include <iostream>
//#include "Pen.h"
#include "Pen.cpp"
This works as intended and correctly prints out "Test successful."
My question is this: what in the world is the point of putting a function prototype in a header file if I have to import the .cpp file later on anyways?
EDIT: It turns out this was a problem with not knowing how to use Code::Blocks rather than with the C++ language.
Assuming you're using gcc, you can compile and link in one step by supplying multiple .cpp files via the command line.
g++ Pen.cpp main.cpp
clang should be similar.
clang++ Pen.cpp main.cpp
An #include should never reference a .cpp file. At all. There's no good reason to do it. Include your headers and then supply the names of all .cpp files when you compile. If your project gets big and you have too many .cpp files to reasonably list, then it's time to break out a makefile or similar.
In the main.cpp include the header file:
#include "Pen.h"
The Pen.h file it's ok.
You need to add the Pen.cpp file to the project tree.
Go to Project -> Add files... and add Pen.cpp
I am new to c++ so forgive me if this question doesn't make much sense, but I have the follow files:
// main.cpp
#include <iostream>
#include <fstream>
using namespace std;
#include "second.h"
int main () {
string filename;
cin >> filename;
ifstream fileIn;
if( !openFile(fileIn, filename) ) {
cerr << "Could not open file \"" << filename << "\"" << endl;
cerr << "Shutting down" << endl;
return -1;
}
return 0;
}
// second.h
#include <string>
#include <fstream>
bool openFile(ifstream& inFile, string filename);
// second.cpp
#include "second.h"
#include <iostream>
#include <fstream>
using namespace std;
bool openFile(ifstream& inFile, string filename) {
inFile.open(filename);
if (inFile.fail()) {
return false;
}
return true;
}
My question is, why does c++ need so much redundancy?
Why do I need to #include <iostream> and <fstream> for both main.cpp and second.cpp if main already calls them? Why does the header files also need to include the function parameters (ifstream& inFile, string filename)? This just seems like a waste, when the function in second.cpp already asks for it. Why not just say openFile(); in the header and have function in second.cpp as for the parameters? This may just be more of a rant, but I just want a deeper understanding as to why theses redundancies are needed. Or is there something I am missing?
The general issue
The reason you have to include relevant headers in each .cpp file is because such .cpp files are compiled separately and independently; in more formal parlance: They constitute different "translation units". See also:
What is a "translation unit" in C++
Sometimes, when multiple translation units must all include several headers, we define a common single header file which containing all these inclusions - so that the .cpp files only need to include the common header to get all their necessary inclusions. Remember that inclusion is basically pasting the contents of the included file instead of the #include directive, and parsing that file as well, so an include-in-an-include is the same as making a direct #include.
Your specific example
In your specific example,
You actually don't need to #include <iostream> in second.cpp, because you only seem to be using constructs from <fstream>.
You don't need to #include <fstream> in any of the .cpp files, because they #include "second.h", which in turn includes <fstream> (so that it gets included when they are compiled, as well).
You can include #include <iostream> and <fstream> into second.h and then include it to main.cpp. Don't forget to use Include Guard, when you link to header files.
main.cpp uses std::ifstream, std::cin, and std::cerr, this is why your example requires the includes in main.cpp.
You should
read about #include directive to understand this idea and what's going on during compilation.
So I'm learning to use a class .h and .cpp files in my program that reads a file containing information about a bank account. Initially the code worked fine, however after creating the .h and .cpp class files, things don't work so smoothly anymore, as I'm getting strange errors that don't make sense to me.
This is my MAIN cpp file:
#include "Bankaccount.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{ string fileName;
cout << "Enter the name of the data file: ";
cin>>fileName;
cout<<endl;
bankAccount object(fileName);
return 0;
}
This is my Bankaccount.h file
#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H
#include <iostream>
#include <fstream>
#include <string>
class bankAccount
{
public:
bankAccount(string n);
bankAccount();
private:
ifstream sourceFile;
}
And lastly this is the Bankaccount.cpp file
#include "Bankaccount.h"
#include <iostream>
#include <fstream>
#include <string>
bankAccount::bankAccount(string n)
{
sourceFile.open(n.c_str());
}
Which is now generating these errors:
include\Bankaccount.h|13|error: expected ')' before 'n'|
include\Bankaccount.h|18|error: 'ifstream' does not name a type|
include\Bankaccount.h|14|note: bankAccount::bankAccount()|
include\Bankaccount.h|14|note: candidate expects 0 arguments, 1 provided|
include\Bankaccount.h|4|note: bankAccount::bankAccount(const bankAccount&)|
include\Bankaccount.h|4|note: no known conversion for argument 1 from 'std::string {aka std::basic_string}' to 'const bankAccount&'|
I think it might be an issue with the headers? I went a little bit crazy and put all of my relevant headers on each file trying to get it to work.
using namespace std;
This is considered a bad programming practice, and you will do yourself a favor if you forget that this is actually a part of C++ language. Although there are proper situations where one would employ using namespace, this should be avoided until one has a much better technical understanding of C++, its structure, and its grammar; in order to recognize and understand when this can be used correctly (if at all).
In your main() you have:
string fileName;
There is no such class in the C++ library whose name is string. The class's correct name is std::string; however by shoving using namespace std; a few lines above, you end up blissfully unaware of this basic, fundamental fact.
Now, after you understand this, let's go back and look at your header file:
ifstream sourceFile;
Well, there's no such class in the C++ library called ifstream, either. The class's proper name is std::ifstream. All classes and templates from the C++ library exist in the std namespace.
However, because when you #included the header file your using namespace std; alias is not yet defined, your compiler doesn't recognize the class name, and you get this compilation error as a reward.
The solution is not to cram a using namespace std; in your header file. That will simply lead to more chaos and confusion. The proper fix is:
Remove using namespace std; from your code, completely.
Use full names of all classes from the C++ library, everywhere. Replace all references to string, ifstream, and everything else, with their actual class names: std::string, std::ifstream, and so on. Get into the habit of explicitly using the std namespace prefix every time. It might seem like a bother at first, but you'll quickly pick up the habit before long, and you won't think of it twice.
And you'll never be confused by these kinds of compilation errors ever agin.
Visual Studio is not recognising my #include 'Header.h' file. I have created the file in the Header Files in solution explorer and also tried manually pointing to the file. What I don't understand is, until yesterday there was absolutely no problem. Therefore, a simple cout doesn't work.
#include 'Header.h';
int main()
{
cout << "hi";
return 0;
}
#include "Header.h"
#include <iostream>
using namespace std;
int main()
{
cout << "hi";
return 0;
}
Not that Header.h is used in anyway, this is still the correct syntax.
You need #include <iostream> to be able to use cout.
You've got syntax errors in your #include preprocessor directive. Replace single with double quotes and drop the semicolon:
#include "Header.h"
Found the problem : I had to go to properties bar and change "Included In Folder" value to true from false.
I'm doing the stanford course cs106b in C++, and I'm stuck and I can't seem to get it right. This probably a very easy fix for someone who knows this kind of stuff.
I have three files, one main.cpp and a randword.h and randword.cpp. In randword.h I have #include "simpio.h" which is a stanford library where GetLine() is defined. I can get GetLine() to work in the main.cpp file but when I try to compile I get "undefined reference to 'GetLine()'" in randword.cpp.
I use codeblocks and I have used the "Add files..." function.
Here's the code for main.cpp:
#include "randword.h"
/* Private function prototypes */
/* Main program */
randword rw;
int main() {
rw.initDictionary();
}
randword.h:
#ifndef RANDWORD_H_INCLUDED
#define RANDWORD_H_INCLUDED
#include <iostream>
#include <fstream>
#include <stdio.h>
#include "simpio.h"
#include "strutils.h"
using namespace std;
class randword{
public:
void initDictionary();
string chooseRandomWord();
string strArray[];
private:
};
#endif // RANDWORD_H_INCLUDED
random.cpp:
#include "randword.h"
using namespace std;
void randword::initDictionary(){
string fileName;
ifstream infile;
fileName = GetLine();
infile.open(fileName.c_str());
if(infile.fail()) cout << "Couldn't read file.";
return;
}
string randword::chooseRandomWord(){
string st1;
return st1;
}
Any help would be much appreciated! I suspect that this question was already posted, but I couldn't find it. Thanks!
try adding the library manually using code blocks
Open up your project
Right click your project and select build options..
select debugger
Go to linker settings
Under Link Librarys click "add"
Find your lib file, select it and keep as a relative path
Your project SHOULD run, if not reply here(as i explained something wrong)
randword.cpp does NOT have the library file needed to use GetLine, you may have included it inside your header file, but this does not carry over to randword.cpp. You need to include the library file just as you would in any other file in order to have access to it's functions.
//randword.cpp
#include <iostream>
#include "simpio.h" //include again!!!
//code here....