This question already has answers here:
Sorting a vector of custom objects
(14 answers)
Closed 1 year ago.
I cannot solve the error - main.cpp:31:34: error: ‘sortBook’ was not declared in this scope in the line sortBook(arr,arr+n,comparator);
I apologize for asking a question that should have a simple solution, but it's driving me nuts. I've checked for all the common errors.
#include<iostream>
using namespace std;
class BOOK{
private:
char bookname[20];
float bookprice;
public:
void getBookDetails()
{
cout<<"Enter the Book Name:";
cin>>bookname;
cout<<"Enter the Book Price:";
cin>>bookprice;
}
void displayDetails()
{
cout<<"Book Name:"<<bookname<<endl;
cout<<"Book Price:"<<bookprice<<endl;
}
};
bool comparator(string a,string b)
{
return a<b;
}
int main()
{
int n=5;
string arr[]={"sandwich","apple","banana","zombie","pear"};
sortBook(arr,arr+n,comparator);
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}
I think it's just that you have not declared sortBook function anywhere and trying to use it.
Your error shows up because you have not defined the method "sortBook".
There are 2 ways to fix this error:
Define your own "sortBook" method in your class.
Use C++ sort method that is defined in "Algorithm". Below is my code that uses this sort method. You have to "include algorithm " at the top of your file, and call the sort method.
#include<iostream>
#include<algorithm> ////// YOU SHOULD INCLUDE THIS STATEMENT
using namespace std;
class BOOK{
private:
char bookname[20];
float bookprice;
public:
void getBookDetails()
{
cout<<"Enter the Book Name:";
cin>>bookname;
cout<<"Enter the Book Price:";
cin>>bookprice;
}
void displayDetails()
{
cout<<"Book Name:"<<bookname<<endl;
cout<<"Book Price:"<<bookprice<<endl;
}
};
bool comparator(string a,string b)
{
return a<b;
}
int main()
{
int n=5;
string arr[]={"sandwich","apple","banana","zombie","pear"};
std::sort(arr,arr+n,comparator); // HERE, YOU CALL : sort()
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}
Note: My code above compiles with no errors. It runs and produces the correct output. Please let me know if you have any other issues.
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
I am getting an lvalue required error at ob1.name and ob2.name. Please
help me figure it out.
#include<iostream.h>
#include<conio.h>
class student
{
public:
int rollno;
char name[10];
};
void main()
{
student ob1;
student ob2;
ob1.rollno=101;
ob1.name="ajay"; // this is where i am getting the error
ob2.rollno=102;
ob2.name="pintu"; // this is where i am getting the error
clrscr();
cout<<"roll no and name of first student"<<ob1.rollno<<ob1.name;
cout<<"roll no and name of second student"<<ob2.rollno<<ob2.name;
getch();
}
You need to use strcpy
i.e.
strcpy(ob1.name, "ajay");
But a better solution would be to use std::string
I was trying to do a simple program to learn a bit more about reference passage, pointers and how to use this 2 on structs in C++ and I got a few questions.
I got an error on the code below "error: 'totalStudent' was not declared in this scope" and my question is, how should I declare the "totalStudent".
#include <iostream>
using namespace std;
struct test{
char name[30];
int age;
};
void addStudent(struct test *ptrTest,int *totalStudent){
for(int i=0;i<2;i++){
cout<<"\nInsert the name: ";
cin.sync();
cin.getline(ptrTest->name,sizeof(ptrTest->name));
cout<<"\nInsert the age: ";
cin.sync();
cin>>ptrTest->age;
*totalStudent+=1;
}
}
void showStudent(struct test *ptrTest,int totalStudent){
for(int i=0;i<totalStudent;i++){
cout<<"\nName: "<<ptrTest->name;
cout<<"\nAge: "<<ptrTest->age;
}
};
int main()
{
struct test t;
addStudent(&t,&totalStudent);
showStudent(&t,totalStudent);
return 0;
}
I can't use pointers and reference passages with structures very well. I can only use them when I'm not using structs.
You forgot to declare this variable in the scope of main:
int main()
{
struct test t;
// LIKE THIS
int totalStudent;
addStudent(&t,&totalStudent);
showStudent(&t,totalStudent);
return 0;
}
in the following code for swapping by call by refernce is used
#include <iostream>
#include <conio.h>
using namespace std;
void swap(int& c,int& d)
{
int temp;
temp=c;
c=d;
d=temp;
}
int main()
{
int a,b;
cout<<" Enter value of a";
cin>>a;
cout<<"\n Enter value of b";
cin>>b;
swap(a,b);
cout<<"a: "<<a;
cout<<"\n b:"<<b;
return 0;
}
but if i use class and make object then there is no need of '&' refernce in calling the method.
#include <iostream>
#include <conio.h>
using namespace std;
class swapy
{
public:
int a;
int b;
void swap(int c,int d)
{
int temp;
temp=c;
c=d;
d=temp;
}
};
int main()
{
swapy s;
cout<<" Enter value of a";
cin>>s.a;
cout<<"\n Enter value of b";
cin>>s.b;
swap(s.a,s.b);
cout<<"a: "<<s.a;
cout<<"\n b:"<<s.b;
return 0;
}
Can you explain how this worked?Why & was not needed in the second program.
First of all you are not calling your class swap() function, because you are not calling it with your class object s, for class swap() function you have to call it with s. like
s.swap(c,d);
Further Change your function swap() name to any other name like mySwap() then you'll get to know that this is not correct, because swap() is a built-in function of std::
so it will be simple to understand if you change the name of your function.
Furthermore, you are not using class member variables correctly, you have to do something with your class member variables a and b. your class is useless here.
I am trying to pass a variable from one function to another. I have tried this approach but it is not working for me:
int c (){
int x1,x2,y2,y1;
system("cls");
cout<<"Insert Value"<<endl
cin>>x1;
return x1;
}
int cd()
{
int a;
a=c();
cout<<"X1: "<<a;
}
Any help is appreciated. Thanks!
There are a few problems with your code.
First of all you are missing a semicolon after the cout statement in your c() function.
Also you have indicated that function cd() should return an int but you are not returning anything.
Lastly, these function will not begin execution unless you call them explicitly.
Try this:
#include <iostream>
using namespace std;
int c (){
int x1,x2,y2,y1;
cout<<"Insert Value"<<endl;
cin>>x1;
return x1;
}
int cd(){
int a;
a=c();
cout<<"X1: "<<a;
return a;
}
int main()
{
int x=cd(); //call the function to create the side effects
return 0;
}