C++ : Member of class not being recognised - c++

I am fairly new to c++ (java background) and I'm trying to access a member of a class I have created but I keep getting an error message when trying to call a member of a class, it's saying that the variable is not a member of the class.
Any ideas why this is happening? I've looked at so many other examples of people with this problem, but none of them have helped me find out why
Main.cpp
#include "stdafx.h"
#include "Adressbuch.h"
#include "Kontakt.h"
#include <iostream>
#include <sstream>
using namespace std;
Adressbuch hinzufügen(Adressbuch buch);
Adressbuch löschen(Adressbuch buch);
void auflisten(Adressbuch buch);
int main()
{
bool end = true;
Adressbuch buch;
while (end) {
cout << "Bitte geben sie ein Aktion ein: (hinzufügen(h)/löschen(l)/beenden(b)/auflisten(a))"
<< endl << "zur Zeit gibt es " << buch.adress_buch.size() << " Kontakte" << endl;
if (cin >> "h") buch = hinzufügen(buch);
else if (cin >> "l") buch = löschen(buch);
else if (cin >> "a") auflisten(buch);
else if (cin >> "b") end = true;
else cout << "Error. Ungultig Eingabe." << endl;
}
return 0;
Adressbuch.h
#include "Kontakt.h"
#include <list>
class Adressbuch{
public:
Adressbuch();
~Adressbuch();
void hinzufügen(Kontakt k);
void löschen(Kontakt k);
list<Kontakt> Adressbuch::adress_buch;
};
Adressbuch.cpp
#include "Adressbuch.h"
#include "Kontakt.h"
#include <list>
using namespace std;
Adressbuch::Adressbuch(){
adress_buch;
}
Adressbuch::~Adressbuch(){
}
void Adressbuch::hinzufügen(Kontakt k){
adress_buch.push_back(k);
}
void Adressbuch::löschen(Kontakt k) {
adress_buch.remove(k);
}
The member that I am having trouble with, is the list adress_buch. Anytime I try to call it, it says its not a member, even though it is defined in the header class?
Error message on line 19 of main()
Severity Code Description Project File Line Suppression State
Error C2039 'adress_buch': is not a member of 'Adressbuch'
ConsoleApplication5 c:\users\gregs\documents\visual studio
2015\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp 19

First a minimal, complete verifiable example that contains nothing but the code required to trigger the error:
#include <list>
using namespace std;
class Adressbuch
{
public:
list<int> Adressbuch::adress_buch;
};
int main()
{
Adressbuch buch;
buch.adress_buch.size();
return 0;
}
That's all that's needed, little bit more than, to find the problem. With nothing else in the way as a distraction problem 1 is easy to spot. I'm no guru in the Visual Studio compiler and I don't have one available, but I'm betting that somewhere in the warnings or errors is this line:
list<int> Adressbuch::adress_buch;
adress_buch is improperly defined causing all sorts of future problems. Compiling this example, GCC gives:
error: extra qualification 'Adressbuch::' on member 'adress_buch'
A corrected example is
#include <list>
using namespace std;
class Adressbuch
{
public:
list<int> adress_buch;
};
int main()
{
Adressbuch buch;
buch.adress_buch.size();
return 0;
}
Or better
#include <list>
class Adressbuch
{
public:
std::list<int> adress_buch;
};
int main()
{
Adressbuch buch;
buch.adress_buch.size();
return 0;
}
Because the notorious, bug-hiding using namespace std; has been removed

Related

Array of objects prints blank string

(The issue has been solved and the solution has been added as comment line to main.cpp)
The problem I'm having is described in the main.cpp file. I already checked another questions about this and none of them really helped.
I'm trying to create a console application with C++ where you can add BOOKS to the LIBRARY. In the library class, there is a displayInfo() function which displays the info of a particular book. It can display integer or double valued informations without having a problem however it is having a trouble when I try to display string typed informations. It just prints blank. Let me give you a little sample of my code.
Here is Book.h
#ifndef BOOK_H
#define BOOK_H
#include <string>
class Book
{
friend class Library;
public:
void setName();
std::string getName();
private:
std::string nameOfBook;
};
#endif
Book.cpp
#include "Book.h"
#include <iostream>
#include <string>
using namespace std;
void Book::setName()
{
string nameOfBook;
cout << "Please enter the name of the book: ";
cin >> nameOfBook;
this->nameOfBook = nameOfBook;
}
string Book::getName()
{
return nameOfBook;
}
Library.h
#ifndef LIBRARY_H
#define LIBRARY_H
#include "Book.h"
#include <array>
class Library
{
private:
std::array <Book , 10> bookArray; // I have to use this
public:
void addBook();
void displayInfo();
};
#endif
Library.cpp
#include "Library.h"
#include "Book.h"
#include <iostream>
#include <string>
#include <array>
using namespace std;
void Library::addBook()
{
bookArray[0].setName();
}
void Library::displayInfo()
{
cout << "The book: " << bookArray[0].getName() << endl;
}
And main.cpp
#include <iostream>
#include "Book.h"
#include "Library.h"
#include <string>
#include <array>
using namespace std;
int main()
{
// The problem is solved
// Create the objects out of the loop
// Library obj1 <---- this solved it
while (true)
{
int number; // Ask user what to do
cout << "Press 1 to add a book\n"
<< "Press 2 to display info\n"
<< "Press 0 to quit\n";
cin >> number;
if (number == 1) // Add a book
{
Library obj1; // <------ THIS IS WRONG
obj1.addBook(); // Consider we named the book as 'Fly'
}
else if (number == 2)
{
Library obj1; // <------ THIS IS WRONG
obj1.displayInfo(); // ******* The output I'm expecting is The Book: Fly
// But instead, it only gives me The Book:
// I have 4 more strings like this in the main project and all of them have the same problem
}
else if (number == 0) // Everything else
{
break;
}
else
{
cout << "Wrong input\n";
continue;
}
}
}
Edit:
I coded this with VS Code and compiled it with MinGW (8.2.0) if it matters.
one problem in your code is that you have many instances of a library class so addBook is landing in one object and displayInfo in a new one (empty one)
you have to:
int main()
{
Library obj1; // declare the lib in scope for all the cases
while (true)
{
int number; // Ask user what to do
cout << "Press 1 to add a book\n"
<< "Press 2 to display info\n"
<< "Press 0 to quit\n";
cin >> number;
if (number == 1) // Add a book
{
obj1.addBook(); // Consider we named the book as 'Fly'
}
else if (number == 2)
{
//Library obj1;
obj1.displayInfo(); // ******* The output I'm expecting is The Book: Fly
// But instead, it only gives me The Book:
// I have 4 more strings like this in the main project and all of them have the same problem
}
else if (number == 0) // Everything else
{
break;
}
else
{
cout << "Wrong input\n";
continue;
}
}
}
u are creating the object again in every iteration of the loop. therefore overwriting the old object that has been given a name.

c++ Class containing vectors of other Classes

I am trying to make a program that has "Class University" that contains a vector for "class Department" [vector Departments]. Inside of the "class Department" under public I have two constructors and a print function.
In my University.cpp, I am trying to make a function that adds a new object Department and puts it into the vector Departments in "University". I believe I am doing this wrong because I am trying to call the constructor for Department() in the University function and I am getting this error message:
$ g++ University.cpp
/usr/li`enter code here`b/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
/tmp/cc6XExwc.o: In function `University::CreateNewDepartment(std::string, std::string, long)':
University.cpp:(.text+0x7d): undefined reference to `Department::Department(std::string, std::string, long)'
collect2: error: ld returned 1 exit status
Class Departments has the following private variables (long id, string name, string location, long chairID). Do I need to use a set() function to create the object, and if so how would I go about doing this?
University.h below
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
#include "Person.h"
#include "Student.h"
#include "Faculty.h"
#include "Department.h"
#include "Course.h"
class University
{
protected:
vector<Department> Departments;
vector<Student> Students;
vector<Course> Courses;
vector<Faculty> Faculties;
public:
University();
~University();
bool CreateNewDepartment(string depName, string depLoc, long depChairId);
University.cpp below
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
#include "University.h"
bool University::CreateNewDepartment (string n, string l, long c)
{
if ( (c != 0) && (!validFaculty (c) ))
return false;
Department D (n, l, c);
Departments.push_back (D);
return true;
}
Department.h
#ifndef DEPARTMENT_H
#define DEPARTMENT_H
#include <string>
#include <iostream>
using namespace std;
class Department
{
friend class University;
protected:
long id;
string name;
string location;
long chairId;
static long nextDepartId;
public:
Department();
Department(string n, string l, long c);
void Print() const;
};
#endif
Department.cpp
#include "Department.h"
using namespace std;
#include <string>
long Department::nextDepartId;
Department::Department()
{
id = chairId = 0;
name = location = " ";
}
Department::Department(string n, string l, long c)
{
name = n;
location = l;
chairId = c;
}
void Department::Print() const
{
cout << "Name: " << name << endl;
cout << "Id: " << id << endl;
cout << "Location: " << location << endl;
cout << "Chair id: " << chairId << endl;
}
As said in the comments by #SteveHolodnak and #M.M., you have two undefined reference problems here.
No main function.
Undefined reference to Department::Department()
Using g++ main.cpp University.cpp Department.cpp where main.cpp is your file with your main function should solve all your problems.

Compiling two projects together in Code Blocks

I'm learning C++ and tutorial asks me to add another project to what I have now.
Also I'm asked to use forward declaration so I can make use of that added file.
Here is my main project:
#include <iostream>
#include "io.cpp"
using namespace std;
int readNumber();
void writeResult(int x);
int main() {
int x = readNumber();
int y = readNumber();
writeResult(x + y);
return 0;
}
here's the added file called io.cpp:
#include <iostream>
using namespace std;
int readNumber() {
cout << "Enter a number: ";
int x;
cin >> x;
return x;
}
void writeResult(int x) {
cout << "Sum of your numbers is " << x << endl;
}
![And here's a screenshot so you can see what error I'm getting which talks about multiple definition and you can see where those two files are added.
According to the tutorial my code is okay but compiler complains. Why ?]1
In codeblocks, when creating a new class, it should automatically header file. Programming with header files is the best practice out there. Here's the code I tried and it worked, with io.h.
main.cpp
#include <iostream>
#include "io.h"
using namespace std;
io inOut;
int main()
{
int x = inOut.readNumber();
int y = inOut.readNumber();
inOut.writeResult(x + y);
return 0;
}
io.h
#ifndef IO_H
#define IO_H
class io
{
public:
int readNumber();
void writeResult(int);
};
#endif
io.cpp
#include <iostream>
#include "io.h"
using namespace std;
int io::readNumber()
{
cout << "Enter a number: ";
int x;
cin >> x;
return x;
}
void io::writeResult(int x)
{
cout << "Sum of your numbers is " << x << endl;
}
I used codeblocks to compile the code written above, and it worked perfectly.
Well as turns out when adding more cpps they're not supposed to be #included on the top. That's what makes compiler say that function is being defined multiple times. All I had to do was just get rid off that one line.
Here's my source:
http://www.cplusplus.com/forum/beginner/44651/

C++ Error: No Member in Class?

I'm working on C++, and this is just a very basic program, but I'm still getting an error.
The error message is:
'class secondary' has no member named 'getting'.
Why is this? It works for my void setting, but not for getting? What am I doing wrong here?
main.cpp
#include <iostream>
#include <string>
#include "secondary.h"
using namespace std;
int main(){
secondary s;
int scale;
cout << "On a scale of 1-10, how awesome are you?" << endl;
cin >> scale;
cout << endl;
s.setting(scale);
cout << s.getting();
return 0;
}
secondary.h
#ifndef SECONDARY_H
#define SECONDARY_H
#include <string>
class secondary
{
public:
void setting(int x);
string getting();
};
#endif // SECONDARY_H
secondary.cpp
#include "secondary.h"
#include <iostream>
#include <string>
using namespace std;
void secondary::setting(int x){
factor = x;
}
string secondary::getting(){
string result;
if(factor < 3){
result = "You have a very low self esteem.";
}elseif(factor > 3){
if(factor > 7){
result = "You have a very high self esteem."
}else{
result = "You have a medium self esteem."
}
}
return result;
}
private factor;
Actually, looking at this again, and deeper, this code has many issues (semicolons missing at key points and the private int definition should have been in the header file, not the cpp file 9t(private is its own section, see below):The problem, from what I can see, s has not yet been instantiated yet, do so and the operation should work correctly.
Please also note that when factor was defined in the cpp file, it was defined at bottom, it should actually be defined before any use of the variable to be defined (in the header file is better meet with common/conventional coding standards).
Please check this tested code:
main.cpp
#include <iostream>
#include <string>
#include "secondary.h"
using namespace std;
int main(){
secondary s;
int scale;
cout << "On a scale of 1-10, how awesome are you?" << endl;
cin >> scale;
cout << endl;
s.setting(scale);
cout << s.getting();
return 0;
}
secondary.h
#ifndef SECONDARY_H
#define SECONDARY_H
#include <string>
class secondary
{
public:
void setting(int x);
std::string getting();
private: // Note: this is how you do private
int factor; // This is the definition with type int, missing in original
};
#endif // SECONDARY_H
secondary.cpp
#include "secondary.h"
#include <iostream>
#include <string>
using namespace std;
void secondary::setting(int x){
factor = x;
}
string secondary::getting(){
string result;
if (factor < 3){
result = "You have a very low self esteem.";
}else if(factor > 3){
if (factor > 7){
result = "You have a very high self esteem.";
}
else{
result = "You have a medium self esteem.";
}
}
return result;
}

Error: Identifier "cout" is undefined. <iostream> included and using namespace std;

I am trying to cout some variables but the compiler says that cout is undefined. I have included iostream and am using namespace std. Removing using namespace std and using std::cout instead changes the issue to "namespace "std" has no member "cout" ". I found some answers saying to add # include "stdafx.h" to the code but Error: cannot open source file "stdafx.h" occurs.
Code is:
#include "Complex.h"
#include <cmath>
#include <iostream>
using namespace std;
Complex::Complex(int PolarOrRectang, float RealOrArg, float ImagOrAng) {
if (PolarOrRectang == 0) {
real = RealOrArg;
imag = ImagOrAng;
else {
real = RealOrArg * cos(ImagOrAng);
imag = RealOrArg * sin(ImagOrAng);
}
};
void Complex::getValue(int PolarOrRectang) {
if (PolarOrRectang == 0) {
cout << real << " +_" << imag << "i" << endl;
} else {
cout << sqrt((real^2) + (imag^2)) << "*e^-" << atan(imag / real)<< endl;
}
};
I'm trying to define a class, so my main is elsewhere.
Running a very basic program that just couts "hello world" works fine, the problem is specific to this code.
Put #include<iostream> at the first position, the order is important
#include "Complex.h"
#include <iostream>
#include <cmath>
PS: Why do you use std:: when you are using "using namespace std;"?