I'm new to C++ and trying just to return values passed through the constructor, I am not sure what I have done wrong on the below code it keeps giving me an error: no instance of constructor..matches, cannot convert parameter 3 from 'const char [5]' to 'int'
#include <iostream>
#include <string>
using namespace std;
class TestClass{
string a;
string b;
int x;
public:
TestClass(string m, string n, int y){
this->a = m;
this->b = n;
this->x = y;
}
int test(){
return this->x;
}
};
int main(){
TestClass *a = new TestClass("test1","test2","9999");
cout<<a->test()<<endl;
}
You are passing the number 9999 as "9999" -- the quotes around it denote it is a string. Simply pass it as 9999.
You have to change your third parameter from "9999" to 9999. The quotes say you're treating it as a string when in fact the constructor is expecting an int.
Related
A program based on constructors
Error -Incompatible Type conversion from char to char[100]
Code -
#include<iostream>
using namespace std;
class demo
{
public:
char name[100];
//This is where char is declared
int marks;
demo()
{
marks = 0;
name = "Pro";
//assigning value to the char
cout<<"Working"<<endl;
}
void input()
{
cout<<"Enter Name and Marks"<<endl;
cin>>name>>marks;
}
void output()
{
cout<<name<<"\t"<<marks<<endl;
}
};
i tried putting name in 'name' and "name"
also i tried using type casting (char)
but it didnt seem to work
int main()
{
demo Obj1, Obj2;
//creating objects
Obj1.output();
Obj2.input();
Obj2.output();
return 0;
}
Arrays do not have the copy assignment operator.
So this statement in the constructor
name = "Pro";
is wrong.
You have to write
#include <cstring>
//...
std::strcpy( name, "Pro" );
Or a simpler way is just to rewrite the constructor like
demo() : name { "Pro" }, mark( 1 )
{
cout<<"Working"<<endl;
}
I would like to ask if it would be possible to declare 2 variables on one line like above (on the same line):
#include <iostream>
#include <string>
using namespace std;
string a, double b;
int main()
{
return 0;
}
Your declaration has the wrong syntax.
string a, double b;
should be string a; double b;.
If you want to declare two variables with a comma they need to have the same type.
Same variable types: YES
Different variable types: NO
Different variables need to be on different lines OR separated with a semicolon ; as it signals a new line e.g.
int a; double b; bool c;
Which is the same as,
int a;
double b;
bool c;
If they're the same type you can use a comma , e.g.
int a, b, c;
Here's my code, and the IDE is DEV C++11
#include<iostream>
using namespace std;
class A{
public:
int a=15;
};
class B:public A
{
};
int main(){
int A::*ptr=&B::a; //OK
int B::*ptr1=&A::a; //why?
int B::A::*ptr2=&B::a;//why?
int B::A::*ptr3=&A::a; //why?
}
I have read Programming Languages — C++ and I know the type of &B::a is int A::*, but I don't realise why the next three lines will pass the compilation.
And the weirdest thing to me is the syntax of int B::A::* , what's the meaning of this? I'm just a newcomer of C/C++, so please tolerate my weird question.
Diagram representation may help you understand why it is ok and compiles
Interesting will be once you reinitialize the same variable in inherited class
#include<iostream>
using namespace std;
class A {
public:
int a = 15;
};
class B :public A
{
public:
int a = 10;
};
int main() {
int A::*ptr = &B::a; //Waring class B a value of type int B::* cannot be
//used to initialize an entity of type 'int A::*'
int B::*ptr1 = &A::a; // why?
int B::A::*ptr2 = &B::a;//Waring class B a value of type int B::* cannot
// be used to initialize an entity of type 'int A::*'
int B::A::*ptr3 = &A::a; //why?
}
I'm teaching myself C++ right now from a book and one of the exercises is to program a class Date that converts a date it holds into a unique integer. However I can't figure out this error I'm getting when I run my program. I'm programming on C++ 2010.
The errors are:
error C2628: 'Date' followed by 'int' is illegal (did you forget a ';'?)
error C3874: return type of 'main' should be 'int' instead of 'Date'
What's odd is I tried to change my main to simply "return 0;" and the above errors still occur. Any ideas?
Here's my code:
#include "stdafx.h"
#include <iostream>
using namespace std;
class Date{
private:
int day, month, year; //declaring variables
public:
//declare constructor
Date(int inputDay=1, int inputMonth=1, int inputYear=2012)
:day(inputDay), month(inputMonth),year(inputYear){};
// declare conversion operator for integers
operator int(){
return year*10000+month*100+day;
}
}
int main() {
Date today(25,11,2012);
return today;
//doesn't matter if I delete above 2 lines and write return 0; both errors still occur
}
You need to add a ; after the class definition.
I hope that the following code answers your query!
#include <iostream.h>
class Date
{
private:
int day, month, year; //declaring variables
long int result;
public:
//declare constructor
Date(int inputDay, int inputMonth, int inputYear)
{
this.day=inputDay;
this.month=inputMonth;
this.year=inputYear;
// declare conversion operator for integers
result=year*10000+month*100+day;
cout<<"Result = "<<result<<"\n";
}
};
int main()
{
Date today(25,11,2012);
return 0;
}
Can't get this code to compile and work properly
The implementation file:
#include <cmath>
#include "quadEquation.h"
using namespace std;
QuadEquation::QuadEquation()
{
int a,b,c;
}
QuadEquation::QuadEquation(int first, int second, int third)
{
a = first;
b = second;
c = third;
}
int QuadEquation::getA()
{
return a;
}
int QuadEquation::getB()
{
return b;
}
int QuadEquation::getC()
{
return c;
}
int QuadEquation::getDiscriminant()
{
return b * b - 4 * a * c;
}
int QuadEquation::getRoot1()
{
discrim = getDiscrimant();
return -b + sqrt(discrim) / (2 * a);
}
int QuadEquation::getRoot2()
{
discrim = getDiscriminant();
return -b - sqrt(discrim) / (2 * a);
}
The header file:
#ifndef QUADEQUATION_H
#define QUADEQUATION_H
class QuadEquation
{
private:
int a, b, c;
public:
QuadEquation(int, int, int);
int getA();
int getB();
int getC();
int getDiscriminant();
int getRoot1();
int getRoot2();
};
#endif
#include <iostream>
#include "quadEquation.h"
using namespace std;
int main()
{
QuadEquation quad1(1,0,9);
cout << "The first root of the first quadratic equation is: " << quad1.getRoot1() << endl;
return 0;
}
Some errors that i am getting
quadEquation.cpp:5: error: prototype for ‘QuadEquation::QuadEquation()’ does not match any in class `QuadEquation`
quadEquation.h:5: error: candidates are: `QuadEquation::QuadEquation(const QuadEquation&)`
quadEquation.h:10: error: `QuadEquation::QuadEquation(int, int, int)`
quadEquation.cpp: In member function `int QuadEquation::getRoot1()`:
quadEquation.cpp:35: error: `discrim` was not declared in this scope
quadEquation.cpp:35: error: `getDiscrimant` was not declared in this scope
quadEquation.cpp: In member function `int QuadEquation::getRoot2()`:
quadEquation.cpp:40: error: `discrim` was not declared in this scope
One of the constructors you've defined is
QuadEquation::QuadEquation()
{
int a,b,c;
}
But this constructor isn't defined in your header file. Moreover, it looks like this is an error on your part, since this constructor doesn't make much sense - it just declares three local variables and doesn't use any of them. If you do want to declare this constructor, add it to your header file, but judging from your code I don't believe it's necessary.
As to your other errors, look at this code:
int QuadEquation::getRoot1()
{
discrim = getDiscrimant();
return -b + sqrt(discrim) / (2 * a);
}
Two things jump out at me. First, where is discrim declared? Second, if quadratic formulas can have arbitrary complex-valued roots, is there a reason you're returning an int? Is there a different type you could use here instead?
Overall, you should learn to read these compiler error messages. Everything I've pointed out could easily have been gleaned from the error output. Now that you're aware what the problems are, can you see how they generate the given compiler errors?
Hope this helps!