Error: Expected initializer before "file", including ifstream. - c++

Not really sure what the error is here. This is standard file opening that I've used all the time before. The right things are being included. And it's just a regular ifstream. What is wrong with this?
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main(){
struct item{
string item;
string type;
int price;
}
ifstream board;
board.open("messageBoard.txt");
}

wow! no one can notice that??!!
int main(){
struct item{ //
string item; // error C2580: redefinition of class name 'item'
string type;
int price;
} // missing a semicolon here `;`
you are using the class name as another identifier so you get a compile-time error redefinition
so you can make them different:
struct Item //
{
string item; // now it's ok Item is not item
string type;
int price;
};

Related

Getting class type redefinition and a few other errors

I'm creating a student data management console application for a project. I created a class called Student which is storing all the data that a student needs to have, and it also has all the getters and setters associated with it. Here is how all my files are laid out:
Student.h
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
string id;
string email;
int presentation;
int essay1;
int essay2;
int project;
public:
//constructor
//Student();
//setters
void set_name(string);
void set_id(string);
void set_email(string);
void set_presentation(int);
void set_essay1(int);
void set_essay2(int);
void set_project(int);
//getters
string get_name();
string get_id();
string get_email();
int get_presentation();
int get_essay1();
int get_essay2();
int get_project();
};
Student.cpp
#include <iostream>
#include <string>
#include "Student.h"
using namespace std;
//constructor definition
/*
Student::Student(void) {
cout << "Student created" << endl;
}
*/
//setter definition
void Student::set_name(string s) {
name = s;
}
void Student::set_id(string s) {
id = s;
}
void Student::set_email(string s) {
email = s;
}
void Student::set_presentation(int a) {
presentation = a;
}
void Student::set_essay1(int a) {
essay1 = a;
}
void Student::set_essay2(int a) {
essay2 = a;
}
void Student::set_project(int a) {
project = a;
}
//getter definition
string Student::get_name() {
return name;
}
string Student::get_id() {
return id;
}
string Student::get_email() {
return email;
}
int Student::get_presentation() {
return presentation;
}
int Student::get_essay1() {
return essay1;
}
int Student::get_essay2() {
return essay2;
}
int Student::get_project() {
return project;
}
Main.cpp
#include <iostream>
#include <string>
#include "Student.h"
using namespace std;
int main() {
cout << "Hello World!" << endl;
Student student1;
Student student2;
Student student3;
student1.set_name("John");
student2.set_name("Bob");
student3.set_name("Carl");
return 0;
}
When I try to run my program, I get, amongst others, the following errors:
Error 1 error C2011: 'Student' : 'class' type redefinition
Error 2 error C2079: 'student1' uses undefined class 'Student'
Error 5 error C2228: left of '.set_name' must have class/struct/union
Error 9 error C2027: use of undefined type 'Student'
How can I go about fixing this issue?
I'm quite sure this is an error caused by the fact that student.h is included twice in a certain .cpp file. Thus you need to use so-called header guards to make sure the file is only included once in every .cpp file:
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>
using namespace std;
class Student {
/* ... */
};
#endif
The idea behind this is that an #include is a preprocessor directive that results in the argument file being copied into the file where the #include was issued. Hence, if files A and B include Student.h, and file C includes both files A and B, then the declaration of class Student is going to end up duplicated. Hence the error. The above macros make sure that this doesn't happen.
Edit as per the question author's comment:
#pragma once is the same as #ifndef .. #define #endif but non-standard .
See #pragma once vs include guards? for reference.
I had the same error. I just clean and rebuild the solution and error resolved.

error C2065 and i have no idea what is wrong

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
class Admin {
static void editUser() {
vector<User> usr = FileManager::createVector(); //errors are here
...
}
};
class FileManager {
public:
static vector<User> createVector() {
string name;
string surname;
string code;
float miles;
float balance;
vector<User> users;
ifstream getUsers("users.txt");
while (getUsers >> name >> surname >> code >> miles >> balance) {
User temp(name, surname, code, miles, balance);
users.push_back(temp);
}
return users;
}
};
This is a piece of code I'm writing and I get these 2 errors:
error C2653: 'FileManager' : is not a class or namespace name
error C3861: 'createVector': identifier not found
The thing is I've looked all over the internet and I really can't see what is wrong, my head hurts a lot, and time is limited. I really didn't want to ask here because you probably have more important questions to answer. Any help is appreciated.
You should either define FileManager before Admin class or use forward declaration to make it visible for compiler.

I get error: C2501 on declaration of strings

#ifndef vid
#define vid
#include<cstring>
#include<string>
#include<string.h>
class Video
{
protected:
string title;
int id;
string genre;
string type;
string actor;
bool available;
public :
virtual double rent();
virtual void displayDetails();
};
#endif
I get this error on declaration of all string attributes.
error C2501: 'string' : missing storage-class or type specifiers
Please help
string is defined under std name space, you could fix your code by providing full namespace:
std::string genre;
std::string type;
std::string actor;
string is included in namespace std.
So either qualify the identifier directly as:
std::string
Or use using directive(which should not be preferred for such limited use)
using namespace std;
string str;
Or use a using declaration
using std::string;
string str;

expected specifier-qualifier-list before std

typedef struct _stResult {
std::string x;
int y;
struct _stResult *next;
} strResult;
In this structure i am getting the following error expected specifier-qualifier-list before std. What does this error mean?
Did you forget to #include <string> ?
The compiler obviously doesn't recognize std::string as a type.
std::string is not declared. If you #include <string> at the top, the code compiles.

error C2146: syntax error : missing ';' before identifier

i can't get rid of these errors... i have semicolons everywhere i checked...
the code is simple:
the error takes me to the definition "string name" in article.h...
main.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
#include "article.h"
int main()
{
string si;
char article[128];
vector<Article> articles;
ifstream file;
file.open("input.txt",ifstream::in);
while(!file.eof())
{
file.getline(article,128);
articles.push_back(Article(article));
}
file.close();
while(1);
return(1);
}
article.h:
#ifndef Article_H
#define Article_H
class Article
{
public:
int year;
string name;
Article(char *i_name);
};
#endif
You should add:
#include <string>
to your "article.h" header file and declare name like this:
std::string name;
It seems the string type is not defined in the artivle.h file. Try to include iostream and add using namespace std (or write std::string instead of using namespace)
You should use the std:: namespace prefix in the header, like
std::string name;