I am new to programming c++. I am trying to read 75 doubles that are inside of a string that I read from a file. I am trying to use istringstream.
This is what I have so far:
Header File:
#ifndef READPOINTS_H_INCLUDE
#define READPOINTS_H_INCLUDE
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std::istringstream;
.....
istringstream linestr;
CPP FILE:
#include
void ReadPoints::grabPoin(const string& read_line, vector<doubles> PointVector){
linestr(read_line);
for(int i = 0; i < 75; i++){
linestr >> value
pointVector.push_back(value);
}
}
When I compile this code I get the following error:
ReadPoints.cpp: In member function ‘bool ReadPoints::grabPoint(const string&, std::vector&)’:
ReadPoints.cpp:48:19: error: no match for call to ‘(std::istringstream {aka std::basic_istringstream}) (const string&)’
linestr(read_line);
Can anyone explain what is wrong and why I am getting the no match for call?
Don't place a definition inside a header. Currently, you've got istringstream linestr;: place this in exactly one *.cpp file, and then in your header, extern std::istringstream linestr (the latter is called a declaration, which is different to a definition). However, this stringstream is best defined in the function itself anyway.
Replace linestr(read_line) in your *.cpp with std::istringstream line_str{ read_line } and remove these two lines from your header file: using namespace std::istringstream; and istringstream linestr;.
Your code should now look like this:
void ReadPoints::grabPoin(const string& read_line, vector<doubles> PointVector){
std::istringstream linestr{ read_line }; // if that doesn't work, use linestr(read_line) instead... note the () and {} have different meanings
for(int i = 0; i < 75; i++){
linestr >> value
pointVector.push_back(value);
}
}
Here's a couple of other tips:
Never place a using directive inside a header (that is, using namespace)
Avoid using directives at all costs. If you don't want to type std::istringstream, place it inside your *.cpp files (yes, each of them), as using std::istringstream.
If you must use a using directive, you need to do it like so: using namespace std;.
Both of these will help you to avoid namespace pollution, which makes calling the right functions difficult.
Related
Im using string library but I can't seem to use the get line function
used both string and string.h but its still not working
I've added the code below and it basically is just to use input numbers in a text file and then using them in a sorting mehthod
the problem is that im only getting the error mentioned below and can't seem to wrap my head around the solution to it
,,assignmentDTS.cpp:34:17: error: no matching function for call to 'getline'
getline(inputFile,tempnumstring);
#include<iostream>
#include<fstream>
#include<string.h>
#include<time.h>
using namespace std;
class sorting {
public:
void bubblesort(int arraySize){
int num;
int *arr=new int[arraySize];
ofstream inputFile("data.txt");
if(inputFile.is_open()){
for (int i = 0; i <arraySize; i++){
string tempnumstring;
std::getline(inputFile,tempnumstring);
num=stoi(tempnumstring);
arr[i]=num;
}
And there we go: ofstream inputFile("data.txt"); needs to be ifstream inputFile("data.txt"); The fstream starts with i for input stream, rather than o for output stream.
Answered on chat
std::getline is declared in #include <string> per the C++ Library Standard.
The #include <string.h> statement includes the C Library Standard string library which has no getline function. This header contains functions like memcpy and strcpy.
The C++ recommended way to reference the C "string.h" header is #include <cstring>.
i have a problem with my program when it compiles. I tried to figure out for like 3-4 hours and still didn't find out how to fix it. The final result is that i want to use struct students in multiple .cpp files without getting the error with multiple definition of.. Can you guys please help me ? Here's the code:
student.h
#ifndef STUDENT
#define STUDENT
#include <string>
using namespace std;
extern int var;
struct students {
char CodSt[20];
string NumeSt;
string PrenSt;
string DenDisc1;
string MedCD1;
string DenDisc2;
string MedCD2;
string DenDisc3;
string MedCD3;
};
#endif
getStudents.cpp
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int var = 0;
extern struct students *student;
void getStudents() {
int i = 0;
ifstream ifs("Curenta.txt");
while(!ifs.eof()) {
ifs >> student[i].CodSt >> student[i].NumeSt >> student[i].PrenSt >> student[i].DenDisc1
>> student[i].MedCD1 >> student[i].DenDisc2 >> student[i].MedCD2 >> student[i].DenDisc3
>> student[i].MedCD3;
if(!ifs.eof()) {
i++;
}
var = i;
}
ifs.close();
}
Compiler error:
In function 'void getStudents()':
[Error] invalid use of incomplete type 'struct students'
[Error] forward declaration of 'struct students'
and same, so on..
Thanks in advance.
If you want to use your struct students you should #include "student.h" file into your .cpp file. What that does is it causes the pre-compiler to "insert" the header file into your source code file, thus providing the correct definition of the struct.
A few side notes about your code (call me pedantic, but it would help you in a long run if you learn these rules early on):
Be consistent in your naming. Your struct is named `students' (plural) but the include file is 'student.h' (singular)
Do not use using namespace ... in your include files. The reason is: what if someone (even you a year from now, when you've forgotten the implementation details of this file) has a different string class with similar semantics but not from the standard library? If such a user includes that students.h file, then "suddenly" all the custom strings become the ones pulled from standard library, ensuring days of fun debugging :)
It is unclear why would you have an extern struct students * student; line in your .cpp file. Either it is a mistake, or (if not) it would be a good idea to provide a short comment explaining why do you need this line
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.
I need to implement a class for one of my assignment and one of the function in the class that has string as datatype doesn't work
my definition code is :
#include <string>
class expression {
public:
expression();
void promptUser();
int getNum1();
int getNum2();
int calculate();
st::string str;
string numToString(int num);
string opToString();
private:
int num1;
int num2;
char op;
};
And in my implementation file
when I try to definite numTostring
string expression::numToString(int num) {
string digit;
...
It says that the declaration is incompatible with the header file(my class definition)
I have no idea why because both the function heading are the same.
the header file of expression.cpp( the implementation file) are :
#include "expression1.h"
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
Your class uses the unqualified name string, but there is no string data type defined in any enclosing scopes. There's a std::string data type defined in namespace std. That's looks to be the type that you need:
std::string str;
std::string numToString(int num);
std::string opToString();
You can keep from having to type out std:: everywhere by specifying a using statement:
using std::string;
But you might not want to do that inside a header file, so stick with fully qualifying the type.
If you want to use , you need to refer to it with std::
For example, your expression class declares:
st::string str;
string numToString(int num);
string opToString();
Which should be:
std::string str; // you typed st:: instead of std::
std::string numToString(int num); // lack of std::
std::string opToString(); // lack of std::
If you dont use 2 files (cpp + h) to define and declare your class then you can add line
using namespace std;
just after your includes. This way you wont have to type std:: each time you try to refer to string and similar types. However, using this is often called a bad "beginner" practice.
If you do use cpp+h then just add std:: before every string type and add using namespace std; to your cpp file.
If you want to know more then read:
1. http://www.cplusplus.com/doc/tutorial/namespaces/
2. Why is "using namespace std" considered bad practice?
3. How do you properly use namespaces in C++?
You also need to move
#include "stdafx.h"
up so it is the first header included. The compiler ignores everything that comes before that magic line.
I have the following class definition, written in C++, residing in it's own header file (ManageFeed.h)
#include <string>
#include <fstream>
#include <iostream>
#include <cstring>
class ManageFeed
{
bool get_feed();
void format_feed();
bool refresh_feed();
int find_start_of_string(string tag, ifstream& rssfile);
public:
void display_feed_list();
void display_feed_item();
ManageFeed();
};
When I try to compile this code, I get the following error
custom-headers/ManageFeed.h:22: error: ‘string’ has not been declared
custom-headers/ManageFeed.h:22: error: ‘ifstream’ has not been declared
I find that I can successfully compile the code without any errors if I remove the parameters from the int find_start_of_string() function, but aren't the parameters required if data is to be passed into the function? If I try to call this function from main(), I receive the following error
reader.cpp:6: error: prototype for ‘void ManageFeed::find_start_of_string(std::string, std::ifstream&)’ does not match any in class ‘ManageFeed’
so they are clearly required for the function to be usable. The textbook I'm using has examples of class definitions in their own head files with parameters present, but there seems to be no other difference in the structure of my code, nor is there any explanation given for why the books code works and mine doesn't.
Question: Are the parameters not required in the definition (the function definitions in ManageFeed.cpp have parameters specified) or am I doing something wrong here?
If anybody's interested, here's my application file
#include "custom-headers/ManageFeed.h"
using namespace std;
ifstream rssfile;
const string tag;
void ManageFeed::find_start_of_string(string tag, ifstream& rssfile);
int main()
{
ManageFeed manage_example;
rssfile.open("rss.xml");
manage_example.find_start_of_string(tag, rssfile);
return 0;
}
and the implementation file for ManageFeed
#include "ManageFeed.h"
#include <iostream>
#include <string>
#include <cstring>
ManageFeed::ManageFeed()
{
}
/*
A function that will get the location of the RSS file from the user
*/
bool ManageFeed::get_feed()
{
cout << "Please specify the location of the feed: " << endl << endl;
cin >> feed_source;
return true;
}
void ManageFeed::store_feed()
{
ifstream source_feed;
source_feed.open(feed_source);
ifstream local_feed;
local_feed.open
(
"/../File System/Feed Source Files/Example Feed/Example Feed.xml"
);
local_feed << source_feed;
}
int ManageFeed::find_start_of_string(string tag, ifstream& rssfile)
{
bool return_value = false;
string line;
size_t found;
do
{
getline(rssfile, line, '\n');
found = line.find(tag);
if (found != string::npos)
{
return_value = true;
return found;
}
} while (!return_value && !rssfile.eof());
if (!return_value)
{
}
}
John has the right solution. Here is the reasoning.
Both string and ifstream live in a namespace called std. When you say string you are telling the compiler to look into the global namespace and find a token called string. There is no such thing. You have to tell the compiler where to find string.
To do so you can either prefix them with std::string and std::ifstream or you can add using namesapce std; at the top of your header file.
Looking a little more closely, you do have the using directive in you .cpp file, but you put it after you include the header. That means the compiler parses the header without the namespace and then parses the rest of the file with it. If you just move the using directive above the header include, it will also fix your problem. Note, however, that anything else using the header will also need to do that same. Thus, start your .cpp file this way:
using namespace std;
#include "custom-headers/ManageFeed.h"
Change:
int find_start_of_string(string tag, ifstream& rssfile);
to:
int find_start_of_string(std::string tag, std::ifstream& rssfile);
Aside: why were there so many questions just like this one today?