I have listed my code below. I get soooo many errors saying cout and endl was not declared in this scope. I do not know what I am doing wrong or how to force the class to recognise cout? I hope I am explaining my problem correctly. If I comment out the methods (not the constructor) it works. I am probably just making a novice mistake here - please help.
using namespace std;
class SignatureDemo{
public:
SignatureDemo (int val):m_Val(val){}
void demo(int n){
cout<<++m_Val<<"\tdemo(int)"<<endl;
}
void demo(int n)const{
cout<<m_Val<<"\tdemo(int) const"<<endl;
}
void demo(short s){
cout<<++m_Val<<"\tdemo(short)"<<endl;
}
void demo(float f){
cout<<++m_Val<<"\tdemo(float)"<<endl;
}
void demo(float f) const{
cout<<m_Val<<"\tdemo(float) const"<<endl;
}
void demo(double d){
cout<<++m_Val<<"\tdemo(double)"<<endl;
}
private:
int m_Val;
};
int main()
{
SignatureDemo sd(5);
return 0;
}
The compiler needs to know where to find std::cout first. You just need to include the correct header file:
#include <iostream>
I'd suggest you not to pollute the namespace using using directives. Instead either learn to prefix std classes/objects with std:: or use specific using directives:
using std::cout;
using std::endl;
Related
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
In C++, it is sometimes considered good practice to declare your classes in a header file and define all the methods in a cpp file. I understand this, but a consequence of this seems to be that instead of having all of the class methods tabbed-in inside curly braces, they are just out in the open in the cpp file. Is there any way to group the methods of a class together in the cpp file while still declaring them in a header file? I like being able to collapse things in my IDE... I'd just get over it, but it's been a while since I've coded anything in C++ and I'm wondering if there's a way to do it that I just forgot about.
To be clear what I mean, here's an example:
test.h:
class Testing {
public:
Testing(int x);
void print();
int x;
};
test.cpp:
#include <iostream>
#include "test.h"
using namespace std;
// class Testing {
// public:
// Testing(int x){
// this->x = x;
// }
// void print(){
// cout << this->x << endl;
// }
// };
Testing::Testing(int x){
this-> x = x;
}
void Testing::print(){
cout << this->x;
}
int main(){
Testing t(100);
t.print();
}
I'd like to do what is commented above in test.cpp instead, but that doesn't work, right? (I think it'd be like declaring a new class distinct from the one in the header file?)
You could do this:
== h.h ==
namespace H_DEFS {
class H {
public:
int A();
int B();
};
}
using namespace H_DEFS;
== h.cpp file ==
#include "h.h"
namespace H_DEFS {
int H::A() { return 4;};
int H::B() { return 5;};
}
== main.cpp ==
#include "h.h"
int main() {
return H().A() + H().B();
}
but it's a weird idiom for other programmers to read just for the benefit of your IDE.
For example, initially I have a sample program:
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
int a[3];
sort(begin(a),end(a));
cin;
}
Now I want to modifystd::cin(to provide more functions like invoke a function when input fails). So I introduces a headermystd.h like:
#include<iostream>
#include<algorithm>
//begin of mystd.h
namespace mystd {
struct cin_wrapper {
}cin;
}
//end of mystd.h
using namespace std;
int main() {
int a[3];
sort(begin(a),end(a));
mystd::cin;
}
But the change seems to be not convenient.(Users must mention all components using std::sort;using mystd::cin; or replace all cin with mystd::cin. using namespace std;using mystd::cin; causes the cin ambiguous)
In fact I'm going to write a modified standard library and make the use of it as convenient as the original one. The ideal code I wish users can write is:
(PS: this means mystd can be just used as std, not indicates I want to encourage users to use using namespace everywhere)
#include<iostream>
#include<algorithm>
#include "mystd.h"
using namespace mystd;
int main() {
int a[3];
sort(begin(a),end(a));//std::sort
cin;//mystd::cin
}
//or
int main() {
int a[3];
mystd::sort(mystd::begin(a),mystd::end(a));//sort, begin, end from std
mystd::cin;
}
I've tried to add using namespace std; in mystd but it also causes ambiguity.
One complicated solution I can image is to create a using statement like using std::string; in mystd for all std members not modified.
Is there a more practical way for me to implement mystd.h?
If you really insist on doing this, you can manage to do so by introducing your using statements at nested scopes. For example:
using namespace std;
int main() {
using namespace mystd;
int a[3];
sort(begin(a), end(a));//std::sort
cin_wrapper w;//mystd::cin
}
Anything involving using namespace std; should be avoided though (using other, more restricted namespaces isn't so bad, but that one is a huge truckload of cans of worms you're opening).
This isn't a good idea, because it is very fragile.
Imagine that someone writes your "ideal" code. Then, one day, you write mystd::sort that accepts a Range instead of two iterators.
Suddenly the meaning of the existing code has changed unexpectedly, and it starts to fail to compile because it wasn't anticipating that the number of parameters should now be one instead of two.
Your requirement is seamlesly implemented by the invention of "namespace".
If "productB" is your product with the same names as in "productA, you want to rewrite; then you decide, which names to be used by your users via some "using" statements in your interface file "productB.h" .
source file productA.h :
namespace productA
{
void f1();
void f2();
}
your source file productB.h : here you decide, what to use :
namespace productB
{
void f1();
void f2();
}
using productA::f1;
using productB::f2;
implementation:
#include <iostream> // std::cout
#include "productA.h"
#include "productB.h"
void productA::f1() { std::cout << "called A::f1" <<std::endl; }
void productA::f2() { std::cout << "called A::f2" <<std::endl; }
void productB::f1() { std::cout << "called B::f1" <<std::endl; }
void productB::f2() { std::cout << "called B::f2" <<std::endl; }
application: very convenient
#include "productA.h"
#include "productB.h"
int main () {
f1();
f2();
}
output:
called A::f1
called B::f2
notice: nothing is ambigous
#include <iostream>
using namespace std;
template<class T>
inline void swap(T &i,T &j)
{
T temp = i;
i=j;
j=temp;
}
int main ()
{
int a = 2,b =5;
swap(a,b);
cout << "a=" <<a<< " b=" <<b<<endl;
return 0;
}
This is a simple swap program using templates. I am new to templates and wanted to learn how to use it.
using namespace std is the problem. There is an std::swap() function that is causing the function call to become ambiguous.
I am trying to work on a tutorial that I wasn't able to finish during class and I'm having a hard time figuring out my errors. I have never seen an explicit qualification error before so I'm not even sure where to start. The only resources I can find online for this kind of error has to do when using namespaces and I don't think I am, at least not explicitly (other than namespace std).
I am sure I'm making a stupid mistake somewhere but these are the errors I'm getting:
View.cpp:12:55: error: explicit qualification in declaration of ‘void promptForAnimals(Animal**, int&)’
View.cpp:53:25: error: explicit qualification in declaration of ‘void printDance(Animal*)’
and this is my promptForAnimals function:
void::promptForAnimals(Animal* barn[], int& numAnimals)
{
//Animal* barn[MAX_ANIMALS];
int num;
string name;
cout << "How many birds? ";
cin >> num; cin.ignore();
for (int i=0; i<num; i++) {
cout << "Name " << i+1 << ": ";
getline(cin, name);
barn[numAnimals++] = new Bird(name);
}
etc
}
}
and my printDanceAnimal is empty, just has:
void::printDance(Animal*)
{
}
The errors could very well have to do with my header file, so here it is for good measure:
#ifndef VIEW_H
#define VIEW_H
#include "Animal.h"
#include "defs.h"
#include <iostream>
using namespace std;
class View
{
public:
View();
~View();
void promptForAnimals(Animal**, int&);
void printDance(Animal*);
};
#endif
You miss class name in these function definitions:
Update:
void::promptForAnimals(Animal* barn[], int& numAnimals)
void::printDance(Animal*)
To:
void View::promptForAnimals(Animal* barn[], int& numAnimals)
void View::printDance(Animal*)
void::promptForAnimals(Animal* barn[], int& numAnimals)
This is wrong. Should be:
void View::promptForAnimals(Animal* barn[], int& numAnimals)
{
// ...
}
This error appears when you explicitly specify already opened namespace.
namespace SomeName {
int SomeName::someFunc() { return 0; } //error
}
I suspect, the empty namespace is the name of the global namespace which is always open, so that is why this kind of error occurs in your case, which is equivalent to that:
int ::someFunc() { return 0; } //error again