Implementation file unable to identify class name from C++ Header file [duplicate] - c++

This question already has answers here:
Can't access function from header file
(2 answers)
Closed 6 years ago.
I am learning C++. I am practising splitting my C++ class into separate files- implementation and header files namely Student_Impl.cpp and Student_Header.h. And then there is a driver file Student_register.cpp which will create 3 Student objects. However, when I try to build it, it's throwing errors stating Student is not a namespace or class. I have included the Student_Header.h (where Student class declaration is present) in my implementation file Student_Impl.cpp and driver file Student_register.cpp but it still throwing the same errors. What can be the possible cause ? I am using Visual Studio 2015.
Student_Header.h
#pragma once
#include <string>
using namespace std;
class Student {
private:
string fname;
string lname;
string address;
string city;
string phone;
int age;
public:
Student();
Student(string, string, string, string, string, int);
~Student();
string get_fname();
string get_lname();
string get_address();
string get_city();
string get_phone();
int get_age();
};
Student_Impl.cpp
#include "Student_Header.h"
#include "iostream"
#include "stdafx.h"
#include <string>
using namespace std;
Student::Student()
{
}
Student::Student(string fname1, string lname1, string address1, string city1, string phone1, int age1)
{
fname = fname1;
lname = lname1;
address = address1;
city = city1;
phone = phone1;
age = age1;
}
string Student::get_fname()
{
return fname;
}
string Student::get_lname()
{
return lname;
}
string Student::get_address()
{
return address;
}
string Student::get_city()
{
return city;
}
string Student::get_phone()
{
return phone;
}
int Student::get_age()
{
return age;
}
Student::~Student()
{}
Student_register.cpp
#include "stdafx.h"
#include "Student_Header.h"
#include <string>
#include "iostream"
using namespace std;
int main()
{
Student Student1("Mike", "J", "MikeAdd", "MikeCity", "MikePhone", 26);
Student Student2("Jack", "R", "JackAdd", "JackCity", "JackPhone", 25);
Student Student3("Roney", "M", "RoneyAdd", "RoneyCity", "RoneyPhone", 27);
// code for data retrieval
return 0;
}

Marvin Sielenkemper put the correct answer in the comment - headers prior to "stdafx.h" are ignored. This is an implementation-specific side effect of how precompiled headers work on Visual Studio.
Also, use #include <iostream> instead of #include "iostream". The <header> form is for standard headers, the "header.h" is for your own headers.

Related

error: request for member in which is of non-class type [duplicate]

This question already has answers here:
How to create an object in a form like this: ifstream in();
(1 answer)
Why can MyClass foo() access private default constructor? [duplicate]
(2 answers)
Trying to understand default constructors and member initialisatioon
(3 answers)
Closed 6 days ago.
error: request for member in which is of non-class type
main.cpp|17|error: request for member 'setName' in 'studentOne', which is of non-class type 'Student()'
the error is on the last line
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
using namespace std;
class Student
{
public:
Student(string name,string program,int age);
void setName(string);
void setProgram(string);
void setAge(int);
string getName();
string getProgram();
int getAge();
private:
string Name;
string Program;
int Age;
};
#endif // STUDENT_H
#include "Student.h"
#include <iostream>
using namespace std;
Student::Student(string name,string program,int age)
{
setName(name);
setProgram(program);
setAge(age);
}
void Student::setName(string name)
{
Name = name;
}
string Student::getName()
{
return Name;
}
void Student::setProgram(string program)
{
Program = program;
}
string Student::getProgram()
{
return Name;
}
void Student::setAge(int age)
{
Age = age;
}
int Student::getAge()
{
return Age;
}
#include <iostream>
#include "Student.h"
using namespace std;
int main()
{
string studentName;
string studentProgram;
int studentAge;
Student studentOne();
cout << "Welcome to the Student input Wizard";
getline(cin, studentName);
cout << endl;
studentOne.setName(studentName);
}
am trying to call the member functions of the class in the main function to input the data for the variable name the others and to print the output.
But every member function I try to call brings about that error, tried the suggestion that I should removes the braces at the end of the object but the the error shows up on that line too.

Implementing a class into a database program

So I am very new to C++ and I am having a hard time learning how too implement classes into a function I have. My prof isn't being helpful in any capacity and I am very lost.
So the jist of the program is to take a file "books.txt" where each line contains an author and a book in the following format: Douglas Adams, The Hitchhiker's Guide to the Galaxy
I am trying to get the function to populate an array with "Book" objects with the title and author data from the file. It takes 4 input arguments: a name of the file as a string, an array of "Book" objects, the number of "Book" objects stored in the array of Book, and the capacity of the library system (with a max of 200).
For each line in the file, I am supposed to instantiate a Book object, fill in the author and title data members (listed in the code below), and append the object to the array of "Book" objects and it will return the numbers of books in the system as an integer.
Here is my header file (Book.h):
#include <iostream>
#include <math.h>
#include <string>
#include <string.h>
#include <fstream>
#include <stdio.h>
#include <cctype>
#include <locale>
#include <algorithm>
using namespace std;
#ifndef BOOK_H
#define BOOK_H
class Book{
private: //Member Variables
string author;
string title;
string inauthor;
string intitle;
string input;
string input2;
public:
Book();
Book(string intitle, string inauthor);
string getTitle();
void setTitle(string input);
string getAuthor();
void setAuthor(string input2);
};
#endif
Here is the .cpp file associated with the header file:
#include <iostream>
#include <math.h>
#include <string>
#include <string.h>
#include <fstream>
#include <stdio.h>
#include <cctype>
#include <locale>
#include <algorithm>
#include "Book.h"
using namespace std;
//Default constructor
Book::Book(){
author = "";
title = "";
}
//Parameterized Constructor
Book::Book(string intitle, string inauthor){
}
//An accessor function that returns the name of the title
string Book::getTitle() {
return title;
}
//A function that assigns the value title to the input given by the user
void Book::setTitle(string title){
title = intitle;
}
//An accessor function that returns the name of the author
string Book::getAuthor() {
return author;
}
//A function that assigns the value author to the input given by the user
void Book::setAuthor(string author){
author = inauthor;
}
And finally, here is the function I am trying to place it into (it is incomplete as every previous attempt I have made to use classes ends in a long list of errors and here is where I can confidently say I can get to):
#include <iostream>
#include <math.h>
#include <string>
#include <string.h>
#include <fstream>
#include <stdio.h>
#include <cctype>
#include <locale>
#include <algorithm>
#include "Book.h"
#include "User.h"
using namespace std;
int readBooks (string filename, int books[] , int bookObj, int capacity){
int i = 0;
ifstream file;
file.open (filename);
if (file.fail()){
return -1;
}
else{
string line;
while ((i < books) && (i < capacity) && (getline(file,line))){
}
}
I bet this is probably a very simple problem but neither the book or any other resources I have been referencing has been able to help me very much. Any help or advice would be greatly appreciated! Thank you!
I can help you out with your class design. It would look something like this:
Book.h
#ifndef BOOK_H
#define BOOK_H
#include <string>
#include <vector>
const int LIBRARY_MAX_CAPACITY = 200;
class Book {
private:
std::string author_;
std::string title_;
public:
Book() : author_( "" ), title_( "" ) {}
Book( const std::string& authorIn, const std::string& titleIn ) :
author_( authorIn ), title_( titleIn )
{}
void setAuthor( const std::string& authorIn ) {
this->author_ = authorIn;
// or just author_ = authorIn;
}
void setTitle( const std::string& titleIn ) {
this->title_ = titleIn;
// or just title_ = titleIn;
}
std::string getAuthor() const { return author_; }
std::string getTitle() const { return title_; }
};
void readBooks( const std::string& filename, std::vector<Book>& books );
#endif // BOOK_H
Book.cpp
#include "Book.h"
#include <fstream>
// this is just pseudo code and will not actually compile
void readBooks( const std::string& filename, std::vector<Book>& books ) {
// open file, test if open correctly
std::ifstream file;
file.open( filename );
// loop through file until end is reached by reading in
// a line of code and getting the contents of the book
while ( file still has data && line <= LIBRARY_MAX_CAPACITY ) {
// get a line of text then parse that line of text.
std::string author = "first string from file before comma"
std::string title = "second string from file after comma"
// create a book object here:
Book book( author, title );
// push back into vector that is passed into this function by reference
books.push_back( book );
}
// done with loop close the file
file.close();
}
Now what ever other function calls this function such as main, or your Library class etc. The std::vector<Book> object will be passed back by reference that is already populated with book objects and std::vector<> has a .size() function that returns it's size as an std::size_t.

Writing to String and Binary Files

Not too many have answered my questions at all. What I want to do a to use private copy constructors and assign them to variables as string or binary files.
Here is the code.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class President
{
private:
President() {};
President(const President&);
const President& operator=(const President&);
string Name;
public:
static President& GetInstance()
{
static President OnlyInstance;
return OnlyInstance;
}
string GetFile()
{
cout<<"Enter the name of the file you want to protect: ";
cin>>Name;
ofstream fsOut;
fsOut.open(Name, ios_base::out, ios_base::binary);
if(fsOut.is_open())
{
cout<<"Writing to binary file."<<endl;
fsOut<<Name<<endl;
cout<<"File open successful."<<endl;
fsOut.close();
}
return Name;
}
void SetFile(string InputFile)
{
Name=InputFile;
}
};
int main()
{
string Name;
President& OnlyPresident=President::GetInstance();
OnlyPresident.SetFile(Name);
cout<<President::GetInstance().GetFile()<<endl;
return 0;
}
I have compiled it and it was fine. However, it was not running correctly or it did not correspond to the file, in which I have typed. How could I protect files correctly using private copy constructors?
John P.

Inheritance error: expected class-name before '{' token

I have tried every combination of #include statements that I can think of, and nothing is working. I am trying to write a basic inheritance program but i keep getting the error error: expected class-name before '}' token and I just do not know what to do about it anymore. I've tried having my main() include the .cpp file of the Executive class, however this error shows up. The program includes 5 types of employees all inherited from the Employee class, and I'm assuming that they are all the same error:
#include <iostream>
#include <string>
#include "Employee.cpp"
#include "Manager.cpp"
#include "Executive.cpp"
#include "Technical.cpp"
#include "Software.cpp"
#include "Test.cpp"
using namespace std;
int main()
{
Employee emp[3];
Executive emp0("John", "Doe", "VP", 100000.0, 1000000.0, 2000.0);
Software emp1("Vincent", "Giuliana", "Project Leader", 150000.0, 200000.0, 1000.0);
Test emp2("Lauren", "Wallis", "Overseer of Testing", 95000, 115000);
emp[0] = emp0;
emp[1] = emp1;
emp[2] = emp2;
for(int i=0; i<3; i++)
emp[i].displayInformation();
emp0.displayInformation();
emp1.displayInformation();
emp2.displayInformation();
return 0;
}
My Employee.h header file is as follows:
#ifndef EMPLOYEE_H_INCLUDED
#define EMPLOYEE_H_INCLUDED
#include <string>
#include <iostream>
using namespace std;
class Employee
{
private:
string fName, lName, jobTitle;
double baseSalary, salary;
public:
Employee();
Employee(string fName, string lName, string jobTitle, double baseSalary);
void calculateSalary(double baseSalary);
void displayName();
void displayBSalary();
void displayJobTitle();
void displayInformation();
...
getters
...
...
setters
...
};
#endif // EMPLOYEE_H_INCLUDED
My Employee.cpp is:
#include <string>
#include <iostream>
#include "Employee.h"
using namespace std;
Employee::Employee()
{
fName = "";
lName = "";
jobTitle = "";
baseSalary = 000000;
}
...
void Employee::setBSalary(double bs) //sets base salary as parameter
{
baseSalary = bs;
}
The top of the Executive.h header class:
#ifndef EXECUTIVE_H_INCLUDED
#define EXECUTIVE_H_INCLUDED
#include <string>
#include <iostream>
//#include "Employee.h"
using namespace std;
class Executive : public Employee
{
private:
string fName, lName, jobTitle;
double baseSalary, salary, bonus, stockOption;
public:
...
};
#endif // Executive_H_INCLUDED
And last but not least, the Executive.cpp file...
#include
#include
#include "Executive.h"
using namespace std;
Executive::Executive()
{
fName = fN;
lName = lN;
jobTitle = jt;
baseSalary = bs;
bonus = b;
stockOption = so;
}
...
void Executive::setSO(double so) //sets stock option as parameter
{
stockOption = so;
}
I think that I have tried to include each header in each file and still, nothing. Any help would be appreciated, and I thank anyone very much in advance!
You must
#include "Employee.h"
in Executive.h, because the compiler must see the declaration of Employee, when a class inherits from it. So, just remove the comments from the #include

an error "has no member named"

I have this snippet of the code
account.cpp
#include "account.h"
#include <iostream>
#include <string>
using namespace std;
Account::Account(string firstName, string lastName, int id)
: strFirstName(firstName), strLastName(lastName), nID(id) {}
void Account::printAccount(){
cout << strFirstName;
}
account.h
#include <string>
using std::string;
class Account{
private:
string strLastName; //Client's last name
string strFirstName; //Client's first name
int nID; //Client's ID number
int nLines; //Number of lines related to account
double lastBill;
public:
Account(string firstName, string lastName, int id);
void printAccount();
};
company.h
#ifndef CELLULAR_COMPANY_H
#define CELLULAR_COMPANY_H
#include <string>
#include <list>
#include <iostream>
#include "account.h"
using namespace std;
class Company {
private:
list<Account> listOfAccounts;
public:
void addAccount(string firstName, string lastName, int id) {
Account newAccount(firstName, lastName, id);
listOfAccounts.push_back(newAccount);
}
void printAccounts(){
for(list<Account>::iterator i = listOfAccounts.begin(); i != listOfAccounts.end(); ++i){
i.printAccount; //here bug
}
}
};
#endif // CELLULAR_COMPANY_H
main.cpp
#include "cellularcompany.h"
int main(){
Company newCompany;
newCompany.addAccount("Pavel", "Nedved", 11111);
newCompany.printAccounts();
return 0;
}
can somebody please explain what does my error mean? thanks in advance (I have it in company.h see comment there)
I have bug 'struct std::_List_iterator<Account>' has no member named 'printAccount'
You forgot the parentheses after printAccount(). Otherwise, it's not a method call. Also, you need to use the -> operator, since it's an iterator.
for(list<Account>::iterator i = listOfAccounts.begin();
i != listOfAccounts.end(); ++i)
{
i->printAccount(); // Note the ()!
// This is equivalent to (*i).printAccount();
}
Try to change i.printAccount; to i->printAccount();