Note in the following code the function called employee_dictionary() has not been created yet. If I highlight the error symbol next to the line number then the options I get from the Eclipse IDE are: function 'employee_dictionary' could not be resolved and 'employee_dictionary' was not declared in this scope.
I assumed I would be presented with the option of having Eclipse create this function automatically to resolve the error. Now I am left wondering if there is something fundamentally wrong with my code or if Eclipse does not have the functionality I am looking for.
I am new to c++ and Eclipse, and I am building this Employee class because it is usually where I start when I want to learn a new language; help resolving the issue would be appreciated. I want an IDE that has this capability so before I get to deep if I need to switch IDE's I will.
#include <iostream>
#include <string>
using namespace std;
class Employee
{
private:
int id;
int salary;
public:
Employee(int new_id, int new_salary)
{
id = new_id;
salary = new_salary;
}
void setID(int newInt)
{
if (employee_dictonary(newInt) == 0)
{
id = newInt;
}
}
int getID()
{
return id;
}
void setSalary(int newInt)
{
salary = newInt;
}
int getSalary()
{
return salary;
}
};
int main()
{
std::cin.get();
}
You need to declare employee_dictionary as a function. You're trying to call a function that hasn't been defined yet. This is the same as trying to use a variable that you haven't defined
Example:
#include <iostream>
#include <string>
using namespace std;
int main()
{
std::cout<<bob; // bob is not declared
}
In function 'int main()':
9:16: error: 'bob' was not declared in this scope
Related
I have been looking in different threads with this error which is quite common but it feels like the IDE I am using messed with my workspace and I can't quite find the problem. I am setting up an extremely basic class called "Movie" that is specified below:
Movie.hpp :
#ifndef MOVIE_HPP
#define MOVIE_HPP
#include <iostream>
#include <string>
using std::string, std::cout,std::size_t;
class Movie
{
private:
std::string name;
std::string rating;
int watched_ctr;
public:
Movie(const string& name, const string& rating, int watched_ctr);
~Movie();
//getters
string get_name() const;
string get_rating() const;
int get_watched() const;
//setters
void set_name(string name);
void set_rating(string rating);
void set_watched(int watched_ctr);
};
#endif // MOVIE_HPP
Movie.cpp:
#include <iostream>
#include <string>
#include "Movie.hpp"
using std::string, std::cout,std::size_t,std::endl;
Movie::Movie(const string& name, const string& rating, int watched_ctr)
: name(name) , rating(rating) , watched_ctr(watched_ctr) {
}
Movie::~Movie()
{
cout << "Destructor for Movies class called /n";
}
//Getters
string Movie::get_name(){return name;}
string Movie::get_rating(){return rating;}
string Movie::get_watched(){return watched_ctr;}
//Setters
void Movie::set_name(std::string n){this -> name = n;}
void Movie::set_rating(std::string rating){this -> rating = rating;}
void Movie::set_watched(int ctr){this -> watched_ctr = ctr;}
The main.cpp I am trying only consists in creating one Movie object:
#include <iostream>
#include <string>
#include "Movie.hpp"
using std::string, std::cout,std::size_t,std::endl;
int main()
{
Movie StarTrek("Star Trek", "G", 20);
}
As you can see, I set all the attribute to private in order to exercise with the set/get methods but I keep stumbling upon the same error on each of them stating >"C:/Users/.../ProjectsAndTests/MoviesClass/Movie.cpp:18:8: error: no declaration matches 'std::__cxx11::string Movie::get_name()"
if you could give me a hint on what might cause this error I would greatly appreciate thank you!
I tried opening another workspace with classes implemented inside of them and the syntax I am using is very close from this test workspace I opened which compiled fine (no error regarding declaration match).
There are 2 problems with your code.
First while defining the member functions outside class you're not using the const. So to solve this problem we must use const when defining the member function outside the class.
Second, the member function Movie::get_watched() is declared with the return type of string but while defining that member function you're using the return type int. To solve this, change the return type while defining the member function to match the return type in the declaration.
//----------------------vvvvv--------->added const
string Movie::get_name()const
{
return name;
}
string Movie::get_rating()const
{
return rating;
}
vvv------------------------------>changed return type to int
int Movie::get_watched()const
{
return watched_ctr;
}
Working demo
Below is code for a simple book list with a class to store book names and isbn numbers into an overloaded function using a vector. This program runs fine and I can test it by returning a specific name (or isbn) using an accessor function from my class.
Question: I tried calling (instantiating?) a constructor with parameters from my class but it would not work, so I commented it out. Yet I was still able to run the program without error. From my main below - //BookData bkDataObj(bookName, isbn);
From watching tutorials, I thought I always had to make an object for a specific constructor from a class that I needed to call? My program definitely still uses my overloaded constructor and function declaration BookData(string, int); without making an object for it in main first.
Thanks for any help or input on this matter.
Main
#include <iostream>
#include <string>
#include <vector>
#include "BookData.h"
using namespace std;
int main()
{
string bookName[] = { "Neuromancer", "The Expanse", "Do Androids Dream of Electric Sheep?", "DUNE" };
int isbn[] = { 345404475, 441569595, 316129089, 441172717 };
//BookData bkDataObj(bookName, isbn); //how did program run without instantiating object for class?
vector <BookData> bookDataArr;
int arrayLength = sizeof(bookName) / sizeof(string);
for (int i = 0; i < arrayLength; i++) {
bookDataArr.push_back(BookData(bookName[i], isbn[i]));
}
cout << "Book 4 is: " << bookDataArr[3].getBookNameCl(); //test if works
return 0;
}
BookData.h
#include <iostream>
#include <string>
using namespace std;
class BookData
{
public:
BookData();
BookData(string, int); //wasn't I supposed to make an object for this constructor in my main?
string getBookNameCl();
int getIsbnCl();
private:
string bookNameCl;
int isbnCl;
};
BookData.cpp
#include "BookData.h"
BookData::BookData() {
bookNameCl = " ";
isbnCl = 0;
}
BookData::BookData(string bookNameOL, int isbnOL) { //how did I use this function
bookNameCl = bookNameOL; //definition without an object in main?
isbnCl = isbnOL;
}
string BookData::getBookNameCl() { //can still return a book name
return bookNameCl;
}
int BookData::getIsbnCl() {
return isbnCl;
}
I’m trying to learn c++ and now i have to face the implementation of a program which ask me to create a list of geeks and a list of projects to work on. For each geek I have to store his id number and his salary, and for each project I have to store the name of it and his Id number.
Then it asks me to associate a specific geek to a specific project.
So my ideas is create a geek.h with information of geek class and the set/get function , than create a project.h with information of project class and set/get function ,and than i would like to implement a addinformation function into a main.cpp and another function to link a specific geek to a project .
now i'm struggling a lot here is my partial code
#include<iostream>
#include<vector>
#include<list>
#include<string>
//geek.h
using std::cout;
using std::cin;
using std::vector;
using std::list;
using std::string;
class geek
{
friend class project;
private:
int id_geek;
double hour_salary;
public:
geek(int i,double h) : id_geek{i},hour_salary{h} {}
void setid(int id)
{
id_geek=id;
}
void seths(double hs)
{
hour_salary=hs;
}
int getid()
{
return id_geek;
}
double geths()
{
return hour_salary;
}
};
#include<iostream>
#include<vector>
#include<list>
#include<string>
#include<iterator>
#include"geek.h"
//project.h
using std::iterator;
using std::vector;
using std::list;
using std::string;
class project: public geek
{
friend class geek;
private:
int id_project;
string project_name;
double total_amount;
double delivery_date;
list<project> projectlist;
public:
project(int id,string pn,double ta,double dd,int idg,double hr) :id_project{id},project_name{pn},total_amount{ta},delivery_date{dd},geek(idg,hr) {}
void setid(int id)
{
id_project=id;
}
void setpn(string pn)
{
project_name=pn;
}
void setta(double ta)
{
total_amount=ta;
}
void setdd(double dd)
{
delivery_date=dd;
}
int getid()
{
return id_project;
}
string getpn()
{
return project_name;
}
double getta()
{
return total_amount;
}
double getdd()
{
return delivery_date;
}
};
#include<iostream>
#include<vector>
#include<list>
#include<string>
#include"project.h"
#include"geek.h"
//fantaware.cpp
using std::vector;
using std::list;
using std::string;
int main()
{
list<geek> geeklist;
list<project> projectlist;
int id ;
double salary;
void Addgeekinfo(geek g)
{
cout <<"insert geek id:";
cin >> id;
cout<<"insert geek salary;";
cin>>salary;
(now how do i use geeklist.push-front(?) in order to put elements into the list??)
(ll have the same prob with aggproject info that's why i did nit read it yet)
(how can i create a function to link a geek to a project)
}
}
`
`
There are some odd things in your code. It looks strange that project inherits from geek and is a friend of geek. Thats not good design and probably you dont need either. project contains a list<project>, thats very odd as well. What is a project? Is it a single project or is it a list of projects? Decide for one and name it accordingly, it cannot be both. Also, you cannot define a function inside a function.
On the other hand, there are also some things that look rather positive in your code: You used the member initialization list, thats a +1! Also you don't use using namespace std; but only have using declarations for things you are actually using. Though having those in headers can be problematic. Not as much as using namespace std; but still any code that includes those headers also includes those using declarations.
Your code is a bit unwieldy, so I allowed myself to reduce it a lot. To push an element to the list you either need to first create an element and then push it (push_front or push_back), or use emplace_front / emplace_back to construct the element in place:
#include<iostream>
#include<list>
struct geek {
int id_geek;
double hour_salary;
geek(int id_geek,double hour_salary) : id_geek(id_geek),hour_salary(hour_salary) {}
};
int main() {
int id ;
double salary;
std::cout <<"insert geek id:";
std::cin >> id;
std::cout<<"insert geek salary;";
std::cin>>salary;
std::list<geek> geek_list;
geek_list.emplace_back(id,salary);
geek g{id,salary};
geek_list.push_front(g);
}
You also might want to provide an overload for operator<< and operator>> so you can write the same more compact:
geek g;
std::cin >> g;
geek_list.push_front(g);
For details I refer you to this answer: https://stackoverflow.com/a/4421719/4117728
If I create a namespace Maths and use another library with a namespace named Maths, will I get an error for re-declaring a variable like PI or re-defining a function like add?
I ask because if I choose to not use namespaces, I should get an error for doing those things, right?
If I don't get an error using namespaces but I do when not using namespaces, isn't safer to just not use namespaces?
Thanks for your time.
First thing, namespace must be global and In different namespace you can have same function or variable, but in same namespace you can't have same variable declaration twice.
namespace A {
int i=10;
char i ='a';/** is not valid **/
void print() {
...
}
}
namespace B {
float i = 1.5; /** valid **/
void print() { /** valid **/
...
}
}
In above example in namespace A and B you have i of int type and i of float type respectively which is valid but In namespace A itself you can't have variable i of different types (considered as re-declaration)
Decided to test it myself:
//in file testnamespace.h
namespace test
{
int t = 1;
void fooo(){};
}
//in file testnamespace2.h
namespace test
{
int t=10000;
void fooo(){int x = -2;}
}
//in file test.cpp
#include "testnamespace.h"
#include "testnamespace2.h"
int main()
{
int ayyyeee = test::t;
foo();
return 0;
}
This gives an error for redefinition,multiple initialization.
All I was looking for.
I'm making a car design program. For this program, we are making a class in a header file, and then we will use that header file in our main program by including the file using #include "name of header".
This is my professors header. This is the code in his header. We were instructed to do the header like he is.
/*
Program 14-2
author - Ray Warren, modified from Language Companion
last updated - 19 July 2013
*/
#include <string>
using namespace std;
class CellPhone
{
private:
// Field declarations
string manufacturer;
string modelNumber;
double retailPrice;
public:
// Constructor
CellPhone(string manufact, string modNum, double retail)
{
manufacturer = manufact;
modelNumber = modNum;
retailPrice = retail;
}
// Member functions
void setManufacturer(string manufact)
{
manufacturer = manufact;
}
void setModelNumber(string modNum)
{
modelNumber = modNum;
}
void setRetailPrice(double retail)
{
retailPrice = retail;
}
string getManufacturer()
{
return manufacturer;
}
string getModelNumber()
{
return modelNumber;
}
double getRetailPrice()
{
return retailPrice;
}
}; //end class
This is the program he used the header file in (as you see he included the header file).
/*
Program14-2
author - Ray Warren, modified from Language Companion
last updated - 19 July 2013
*/
#include <iostream>
#include <cstdlib>
#include "CellPhone.hpp"
using namespace std;
int main()
{
// Create a CellPhone object and initialize its
// fields with values passed to the constructor.
CellPhone myPhone("Motorola", "M1000", 199.99);
// Display the values stored in the fields.
cout << "The manufacturer is "
<< myPhone.getManufacturer() << endl;
cout << "The model number is "
<< myPhone.getModelNumber() << endl;
cout << "The retail price is "
<< myPhone.getRetailPrice() << endl;
system("Pause");
return 0;
} //end main
This is my header file containing my class.
#include <string>
using namespace std;
Car(int ym, string mk)
{
yearModel=ym;
make=mk;
speed=0;
}
void setYearModel(int ym)
{
yearModel=ym;
}
void setMake (string mk)
{
make=mk;
}
int returnYearModel()
{
return yearModel;
}
string returnMake()
{
return make;
}
int returnSpeed()
{
return speed;
}
void accelerate()
{
speed += 5;
}
void brake()
{
This is my main program with me attempting to include the header file into my main program. When I tried to compile this, my header file popped up in a new code blocks IDE tab, and gave me this list of errors.
http://prntscr.com/bzu4x5 <--------- list of errors
I have no idea what I'm doing wrong. From what I see, I copied my professor exactly as he told us to, and I'm still getting errors.
Does anyone have any ideas of what's causing this massive list of errors?
#include <string>
#include <iostream>
#include "Car header.h"
int main()
{
}
Your "header file" contains the definitions of the class member functions. It should actually be a .cpp file. What you are missing is a definition of the class itself, and declarations of the member functions.
Note that your professor's sample header file defines the member functions inside the class definition. This is actually poor practise, but he may not have got round to teaching you how do define functions out of line yet.
If you are going to define the functions out of line, you will also need to change the functions to some thing like:
std::string Car::returnMake()
{
return make;
}
Note: "using namespace std" is also poor practise. Professional C++ code tends to be explicit, and use the "std::" prefix. Just get in the habit and save yourself hours of pain.
You did not actually define a class in you header file. Notice that in your teacher's header file, he has:
class Cellphone // -> this is what you do not have
{
private:
// Field declarations
string manufacturer;
string modelNumber;
double retailPrice;
public:
//constructor function
CellPhone(string manufact, string modNum, double retail);
// Member functions
void setManufacturer(string manufact);
void setModelNumber(string modNum);
void setRetailPrice(double retail);
string getManufacturer();
string getModelNumber();
double getRetailPrice();
}
You should have you fields and member functions inside your class. Hope that helps