c++ ifstream undeclared identifier - c++

I am in Visual Studio and am getting 'ifstream undeclared identifier' with this code (same for ofstream)
#include <iostream>
#include <iomanip>
#include <fstream>
void main()
{
ifstream infile("file.txt");
ofstream outfile("out.txt");
}
do I need to include something else?

You need to scope it. Use using namespace std; or preface ifstream and ostream with std::
For example, std::ifstream
Currently, the compiler does not know where these structures are defined (since they are declared/defined within the std namespace). This is why you need to scope your structures/functions in this case.

You need to reference the standard namespace (std). Try this:
#include <iostream>
#include <iomanip>
#include <fstream>
void main()
{
std::ifstream infile("file.txt");
std::ofstream outfile("out.txt");
}

You can use
using namespace std;
instead of prefixing everyline with std::

Related

Invalid Template Argument for vector instantiation

When I run this code I get "invalid template arguments" error on the last line. Please advise. (I've omitted the rest of the code)
#include <iostream>
#include <fstream>
#include<array>
#include <vector>;
using namespace std;
int fileLineCount(string);
int fileExists(string[],int);
int main() {
ifstream archiveFile;
archiveFile.open("StudentRecords.txt");
int lineCount=fileLineCount("StudentRecords.txt");
string line;
vector<string> recordArray;
#include <vector>;
should be
#include <vector>
and of course you need to close the } brace at the end of main() (although probably this was a typo). You should also #include <string>, although some of your headers seem to include it implicitly (probably <iostream>).

"string is not a type" in namespace

I write a program in c++ with two files.
main.cpp
#include "var.hpp"
#include <iostream>
using namespace std;
using namespace YU;
int main()
{
string god = "THL";
age = 10;
cout << age << endl;
cout << god << endl;
return 0;
}
var.hpp
#ifndef __VAR_H__
#define __VAR_H__
#include <string>
namespace YU
{
int age;
string name;
}
#endif
When I compilered it, It'get wrong.
the wrong infomation is:
In file included from main.cpp:1:0:
var.hpp:9:5: Error: ‘string’ is not a type name
I don't know why,I had include <string> head file, but it still dosen't work.
I write this code just for practice, not for work.
thank you!
The problem is the namespace of string in var.hpp. string is the std namespace, but you are not telling the compiler that. You could fix it by putting using namespace std; in var.hpp, but the following is a better solution as it doesn't clutter the global namespace with other things from std:
#ifndef __VAR_H__
#define __VAR_H__
#include <string>
namespace YU
{
int age;
std::string name;
}
#endif
You have using namespace std; in your .cpp file, but it comes after the include of var.h. If you're going to write the header like that, you should put using namespace std; in the header as well.
Alternativly you could use
using std::string;
This avoids having to type std::string in front of every string, and you don't grab everything from the global namespace.

identifier "string" undefined?

I am receiving the error: identifier "string" undefined.
However, I am including string.h and in my main file, everything is working fine.
CODE:
#pragma once
#include <iostream>
#include <time.h>
#include <string.h>
class difficulty
{
private:
int lives;
string level;
public:
difficulty(void);
~difficulty(void);
void setLives(int newLives);
int getLives();
void setLevel(string newLevel);
string getLevel();
};
Can someone please explain to me why this is occurring?
<string.h> is the old C header. C++ provides <string>, and then it should be referred to as std::string.
You want to do #include <string> instead of string.h and then the type string lives in the std namespace, so you will need to use std::string to refer to it.
You forgot the namespace you're referring to. Add
using namespace std;
to avoid std::string all the time.
Because string is defined in the namespace std. Replace string with std::string, or add
using std::string;
below your include lines.
It probably works in main.cpp because some other header has this using line in it (or something similar).
Perhaps you wanted to #include<string>, not <string.h>. std::string also needs a namespace qualification, or an explicit using directive.
You must use std namespace. If this code in main.cpp you should write
using namespace std;
If this declaration is in header, then you shouldn't include namespace and just write
std::string level;
#include <string> would be the correct c++ include, also you need to specify the namespace with std::string or more generally with using namespace std;

std has no member 'getline'?

I'm trying to use std::getline, but my compiler is telling me that getline isn't identified?
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <fstream>
#include <cstdlib>
int main(){
using namespace std;
string line;
ifstream ifile("test.in");
if(ifile.is_open()){
while(ifile.good()){
getline(ifile,line);
}
}
}
std::getline is defined in the string header.
#include <string>
Also, your code isn't using anything from cstring, cstdio, cmath, or cstdlib; why bother including these?
EDIT: To clarify the confusion regarding the cstring and string headers, cstring pulls the contents of the C runtime library's string.h into the std namespace; string is part of the C++ standard library and contains getline, std::basic_string<> (and its specializations std::string and std::wstring), etc. -- two very different headers.
As ildjarn points out, the function is declared in <string>, and I'm suprised you didn't get an error at:
string line;
Also, this:
while(ifile.good()){
getline(ifile,line);
}
is not the way to write a read loop. You MUST test the success of the read operation, not the current stream state. You want:
while( getline(ifile,line) ) {
}
this is happening because getline comes from the string library, you need to #include <string> or #include <cstring>

String Undeclared In C++

I'm sure this is a really simple thing, but I haven't worked in C++ forever.
14 C:\Dev-Cpp\mainCurl.cpp `string'
undeclared (first use this function)
> #include <stdio.h>
> #include <curl/curl.h>
> #include <string>
> #include <iostream>
>
> int main(void) {
> string url("http://www.google.com"); //
> system("pause");
>
> return 0; }
What am I missing here?
Cheers
You haven't declared your namespace. You need to either declare:
using namespace std;
Or tell the compiler that "string" is in the standard namespace:
std::string url("...");
Or you can announce that you are specifically using std::string and only std::string from std by saying:
using std::string;
Add using namespace std; above the main() definition.
Also, you don't need <stdio.h> if you include <iostream>. Also, in C++ a function that doesn't take arguments doesn't need a "void" argument, simply use parentheses with nothing in between them.
This a so recurring problem...
You missed std:: before string, so it will look like std::string
That's because string belongs to std namespace and if you don't use using directive you must specify where string is.
Alternatively you can use
using namespace std; or more conveniently using std::string before using string class.
You need std::string or using std::string.
try std::string url ("http://www.google.com");
the string class is in std namespace