I've been trying to learn about parametrised constructors:
Here is the program I wrote:
#include <iostream> //Using a parametrised constructor i.e. we give an integer value as a parameter
using namespace std;
class item{
int cost;
int price;
public:
item (int a){
cost=a;
}
void display(){ //Display is not a constructor, hence we need to specify its return type and parameters
cout << cost;
}
} item1;
int main(){
item1(5);
item1.display();
return 0;
}
However, I get an error on Visual Studio Code:
call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type
Can anyone tell me what's wrong with the code?
At the end of class creation you are asking to create an object named item1 but you are not providing any value, that's why it is throwing error you cannot create an object without '('.
After object creation you can't call the constructor. you can access all the methods and fields.
#include <iostream> //Using a parametrised constructor i.e. we give an integer value as a parameter
using namespace std;
class item{
int cost;
int price;
public:
item (int a){
cost=a;
}
void display(){ //Display is not a constructor, hence we need to specify its return type and parameters
cout << cost;
}
} item1(5);
int main(){
item1.display();
return 0;
}
In following code, i get an error on line 33. Why? What is the correct syntax?
Surely I made some stupid mistake ... unfortunately I'm trying to better understand the vectors.
#include <iostream>
#include <vector>
class firstClass
{
public:
firstClass(int x, int y):sum(x+y)
{
}
void getSum()
{
std::cout << sum << std::endl;
}
private:
int sum;
};
class secondClass
{
public:
secondClass(int dim)
{
obj = new std::vector<firstClass>(dim,firstClass{3,5});
}
private:
std::vector<firstClass>*obj;
};
int main()
{
secondClass*obj2;
obj2 = new secondClass(4);
obj2->(*obj)[0].getSum(); //HERE!
return 0;
}
Error:
error: expected unqualified-id before '(' token
error: 'obj' was not declared in this scope
The correct syntax for accessing the data member should be:
(*(obj2->obj))[0].getSum();
Note that secondClass::obj is private data member, so you can't access it in main().
For code sample you showed, you don't need to use raw pointer and new at all.
Newbie to C++ over here. I am trying to create 2 classes and interlink them using Composition, but I keep getting errors.
#include <iostream>
#include <string>
using namespace std;
class student
{
public: int roll_no;
string name;
string dob;
void student_display()
{
cout<<roll_no<< " "<<name<<" "<<dob<<endl;
}
student(int roll,string names,string dateofb)
{
roll_no=roll;
name=names;
dob=dateofb;
}
~student(){};
};
class course
{
public:string course_name;
int duration;
void course_display()
{
cout<<course_name<< " "<<duration<<" "<<endl;
s1.student_display()<<endl;
}
course (string c_name,int dur,student s2):course_name(c_name),duration(dur),s1(s2){}
~course(){};
private : student s1;
};
class college
{
public: string college_name;
string location;
course c1;
course (string col_name,string loc,course c2):college_name(col_name),location(loc),c1(c2){}
~course(){};
};
int main() {
student s5(001,"Noel","28/04/1994");
s5.student_display();
course c1("Engineering",4,s5(001,"Noel","28/04/1994"));
c1.course_display();
return 0;
}
The errors are as follows:
prog.cpp: In member function 'void course::course_display()':
prog.cpp:32:35: error: invalid operands of types 'void' and '<unresolved overloaded function type>' to binary 'operator<<'
s1.student_display()<<endl;
^
prog.cpp: At global scope:
prog.cpp:45:20: error: expected ')' before 'col_name'
course (string col_name,string loc,course c2):college_name(col_name),location(loc),c1(c2){}
^
prog.cpp:47:13: error: declaration of '~course' as member of 'college'
~course(){};
^
prog.cpp: In function 'int main()':
prog.cpp:52:54: error: no match for call to '(student) (int, const char [5], const char [11])'
course c1("Engineering",4,s5(001,"Noel","28/04/1994"));
Could someone please help me out? I have tried going through forums with the same error but cant figure it out
^
I have modified the code as follows:
#include <iostream>
#include <string>
using namespace std;
class student
{
public: int roll_no;
string name;
string dob;
void student_display()
{
cout<<roll_no<< " "<<name<<" "<<dob<<endl;
}
student(int roll,string names,string dateofb)
{
roll_no=roll;
name=names;
dob=dateofb;
}
~student(){};
};
class course
{
public:
string course_name;
int duration;
private:
student s1;
public:
void course_display()
{
cout<<course_name<< " "<<duration<<" "<<endl;
s1.student_display();
}
course (string c_name,int dur,student s2):course_name(c_name),duration(dur),s1(s2){}
~course(){};
//private: // shifted before course_display()
// student s1;
};
// Not used
//class college
//{
//public:
// string college_name;
// string location;
// course c1;
//
// course (string col_name,string loc,course c2):college_name(col_name),location(loc),c1(c2){}
//
// //~course(){}; // destructor mismatch
//};
int main() {
student s5(001,"Noel","28/04/1994");
s5.student_display();
//course c1("Engineering",4,s5(001,"Noel","28/04/1994"));
course c1("Engineering",4,s5);
c1.course_display();
return 0;
}
and the output is follows:
Line 30: Remove the semicolon.
Line 45: Looks like copy/paste error, should be the constructor of the college class, right? Then change the name from course to college. Same for the destructor on the following line.
Line 52: You create the object s5 2 lines above, I guess you want to pass this object to the constructor of the course object. If so then just remove the expression in the brackets after s5.
I get this error when trying to push back an object of custom type. code follows:
class Item_base
{
public:
Item_base(string isbn=" ", int num=0):book(isbn),number(num){}
Item_base(Item_base &I):book(I.book),number(I.number){cout<<"\nCopy Constructor"<<endl;}
Item_base &operator=(Item_base &d){if (this != &d){this->book=d.book;this->number=d.number;cout<<"\nAssignment Operator"<<endl;return *this;}else{cout<<"\nAssignment Operator"<<endl;return *this;}}
~Item_base(){cout<<"Item_base Destructor"<<endl;}
protected:
string book;
int number;
};
#include <iostream>
using namespace std;
#include <vector>
#include "Item_base.h"
int main (int argc, char * const argv[]) {
// insert code here...
vector<Item_base> vecbase;
Item_base derivo("Harry Potter",10);
cout<<"enter book name, qty, dqty"<<endl;
vecbase.push_back(derivo);
return 0;
}
The error message i get is:
error: no matching function for call to 'Item_base::Item_base(const Item_base&)'
Can someone help me with this? I am new to programming
Vector has only two implementations of push_back function
push_back(const T&)
push_back(T&&) (since C++11)
This explains, why we need to provide copy constructor with parameter const T& for our class
Here is my code
#include <iostream>
using namespace std;
class MyTestClass
{
int MyTestIVar;
public:
MyTestClass(void);
int firstCallMethod(void);
int secondCallMethod(void);
};
MyTestClass::MyTestClass(void)
{
MyTestIVar = 4;
}
int MyTestClass::firstCallMethod(void)
{
return secondCallMethod();
}
int MyTestClass::secondCallMethod(void)
{
return MyTestIVar;
}
int main(int argc, char *argv[])
{
MyTestClass mTC;
cout << mTC.firstCallMethod() << endl;
return 0;
}
If use use
MyTestClass mTC();
instead it will disallow me to call any member functions and display this error
./experiment.cpp: In function ‘int main(int, char**)’:
./experiment.cpp:31:14: error: request for member ‘firstCallMethod’ in
‘mTC’, which is of non-class type ‘MyTestClass()’
I read the posts on value-initialize etc, but this error still doesn't seem logical to me. Why would this affect member functions?
And help greatly appreciated :-)
MyTestClass mTC();
Does not declare an object of the MyTestClass class, as you think.
It Actually, declares a function by the name of mTC which does not take any parameters and returns an MyTestClass object.
This is known as the Most Vexing Parse in c++.
You have stumbled upon the most vexing parse.
The line
MyTestClass mTC();
is parsed as a function prototype of a function named mTC which has no arguments and returns an instance of MyTestClass.