I want to pass the pointer to a structure(variables of this structure is an array with elements that have value x and y) to the constructor. Next I want to assign values x and y of each variable of this structure to the similar variable values of the structure in the class.
class Convex_quadrliteral
{
protected:
struct VC {
float x, y;
} vertice_coordinate[4];
public:
Convex_quadrliteral (VC *pointerVC);
};
Convex_quadrliteral::Convex_quadrliteral (VC *pointerVC) {
cout << "\nObject is being created" << endl;
for (int i = 0; i < 4; i++) //variable initialisation
{
vertice_coordinate[i].x = pointerVC[i].x;
vertice_coordinate[i].y = pointerVC[i].y;
}
//object's properties output
cout << "Properties: " << endl
<< "A (" << vertice_coordinate[0].x << ", " << vertice_coordinate[0].y << ")" << endl
<< "B (" << vertice_coordinate[1].x << ", " << vertice_coordinate[1].y << ")" << endl
<< "C (" << vertice_coordinate[2].x << ", " << vertice_coordinate[2].y << ")" << endl
<< "D (" << vertice_coordinate[3].x << ", " << vertice_coordinate[3].y << ")" << endl;
}
int main()
{
struct vertice_coordinate
{
float x, y;
};
vertice_coordinate *pointerVC = new vertice_coordinate[4];
for (int i = 0; i < 4; i++) {
pointerVC[i].x = 2;
pointerVC[i].y = 2;
}
Convex_quadrliteral figure_1(pointerVC);
I expect the output:
A(2, 2)
B(2, 2)
C(2, 2)
D(2, 2)
The output is error: no declaration matches 'Convex_quadrliteral::Convex_quadrliteral(Convex_quadrliteral::VC*)'C onvex_quadrliteral::Convex_quadrliteral (VC *pointerVC)
You re-define your VC struct, and even though the struct looks identical, the compiler will treat them as two different types. Define one struct and use it in both your class and in main.
Related
#include <iostream>
using namespace std;
void swap(int& a, int& b)
{
cout << "address of a: " << &a << " value of a: " << a << endl;
cout << "address of b: " << &b << " value of b: " << b << endl;
int tmp{move(a)};
cout << "address of tmp: " << &tmp << " value of tmp: " << tmp << endl;
a = move(b);
b = move(tmp);
cout << "address of a: " << &a << " value of a: " << a << endl;
cout << "address of b: " << &b << " value of b: " << b << endl;
}
void swap_no_move(int& a, int& b)
{
cout << "address of a: " << &a << " value of a: " << a << endl;
cout << "address of b: " << &b << " value of b: " << b << endl;
int tmp{ a };
cout << "address of tmp: " << &tmp << " value of tmp: " << tmp << endl;
a = b;
b = tmp;
cout << "address of a: " << &a << " value of a: " << a << endl;
cout << "address of b: " << &b << " value of b: " << b << endl;
}
int main() {
int a = 10;
int b = 5;
swap(a, b);
cout << endl;
int c = 10;
int d = 5;
swap_no_move(c, d);
cin.get();
return 0;
}
I have two swap functions: swap and swap_no_move. According to what I read from the book, there should be no "copy" in function swap which means the address of tmp should be the same for tmp and an in function swap. However, the output I got shows there is no difference between these two functions, did I do something wrong?
The definition
int tmp{move(a)};
doesn't move the reference or the variable a itself. It creates a brand new variable tmp which the compiler allocates space for. Then the value of a is moved into tmp.
And since moving int values can't really be done, it's exactly the same as
int tmp = a;
This question already has answers here:
C++: Argument Passing "passed by reference"
(4 answers)
Why is the copy constructor called when we pass an object as an argument by value to a method?
(3 answers)
Closed 6 years ago.
The program should resolve grade 1 equations in this specific manner. I had to use output messages for the constructors and destructors for better understanding of the code.
#include "stdafx.h"
#include <iostream>
using namespace std;
class Ec {
public: float a, b; //equation's parameters
public:
Ec() {float x, y; cout <<"a= "; cin >> x; cout << "b= ";
cin >> y; a = x; b = y; cout << "void constr\n";};
Ec(float x, float y) { a = x; b = y; cout << "param constr\n"; }
~Ec() { cout << "destr\n"; }
Ec(Ec &z) { a = z.a; b = z.b; cout << "cpy constr\n"; }
friend float half1(Ec); //function to return a/2
friend float half2(Ec); //function to return b/2
friend float sol1(Ec); //function to return the solution for the standard eq
friend float sol2(Ec); //function to return the sol for the /2 param eq
};
float half1(Ec ec1) { return (ec1.a / 2);}
float half2(Ec ec1) { return (ec1.b / 2); }
float sol1(Ec ec1) { float x; return x = -ec1.b / ec1.a; }
float sol2(Ec ec1) { float x2; return x2 = -half2(ec1) / half1(ec1); }
int main()
{
int x, y;
cout << "a= ";
cin >> x;
cout << "b= ";
cin >> y;
Ec ec1;
Ec ec2(x, y);
Ec ec3 = ec1;
//the couts display for ex:" ec 2x+1=0 has sol -0.5"
cout << "ec " << ec1.a << "x+ " << ec1.b << "=0 has sol " << sol1(ec1) << endl;
cout << "ec " << ec2.a << "x+ " << ec2.b << "=0 has sol " << sol1(ec2) << endl;
cout << "ec " << ec3.a << "x+ " << ec3.b << "=0 has sol " << sol1(ec3) << endl;
cout << "ec halved " << half1(ec1) << "x+ " << half2(ec1) << "=0 has sol " << sol2(ec1) << endl;
cout << "ec halved " << half1(ec2) << "x+ " << half2(ec2) << "=0 has sol " << sol2(ec2) << endl;
cout << "ec halved " << half1(ec3) << "x+ " << half2(ec3) << "=0 has sol " << sol2(ec3) << endl;
}
return 0;
}
Now, I have troubles understanding why after the first cpy constructor(for ec3) another cpy constructor is called then a destructor. What is it doing?
Your functions take Ec objects by value
float half1(Ec ec1) { return (ec1.a / 2);}
^
These will make function local copies that are then destroyed at the end of each function.
If you want to avoid making these copies, pass the arguments by const reference
float half1(Ec const& ec1) { return (ec1.a / 2);}
^
I'm trying to execute the following code:
#include <iostream>
using namespace std;
class ABC {
private:
int x, y;
public:
ABC(){
cout << "Default constructor called!" << endl;
ABC(2, 3);
cout << x << " " << y << endl;
}
ABC(int i, int j){
cout << "Parameterized constructor called with parameters "<< i << " " << j << "!" << endl;
x = i;
y = j;
cout << x << " " << y << endl;
}
};
int main(){
ABC a;
return 0;
}
I am getting the following output:
Default constructor called!
Parameterized constructor called with parameters 2 3!
2 3
-858993460 -858993460
Shouldn't the member variables be initialized with values 2 and 3?
ABC(2, 3); doesn't call the constructor to initialize the members, it just create a temporary variable which will be destroyed immediately.
If you meant delegating constructor you should:
ABC() : ABC(2, 3) {
cout << "Default constructor called!" << endl;
cout << x << " " << y << endl;
}
Note this is a C++11 feature. You can add a member function if you can't use C++11.
class ABC {
private:
int x, y;
init(int i, int j) {
x = i;
y = j;
}
public:
ABC(){
cout << "Default constructor called!" << endl;
init(2, 3);
cout << x << " " << y << endl;
}
ABC(int i, int j){
cout << "Parameterized constructor called with parameters "<< i << " " << j << "!" << endl;
init(i, j);
cout << x << " " << y << endl;
}
};
You create a temporary variable in ABC() body. You can use this syntax to overcome this:
class ABC
{
private:
int x, y;
public:
ABC() : ABC(2,3)
{
std::cout << "Default constructor called!" << std::endl;
}
ABC(int i, int j)
{
std::cout << "Parameterized constructor called with parameters "<< i << " " << j << "!" << std::endl;
x = i;
y = j;
std::cout << x << " " << y << std::endl;
}
};
I'm trying to make it so every time the user inputs a value, it's stored in an array in a place that represents a stat. Hard to explain so here's the code:
void MainChar::CharacterCreation()
{
int statPoints = 20;
int health = 0;
int magicDamage = 0;
int magicResist = 0;
int physicalResist = 0;
int physicalDamage = 0;
int magicOffMastery = 0;
int physicalOffMastery = 0;
int magicDefMastery = 0;
int physicalDefMastery = 0;
// SETS STATS AND THIER RESPECTIVE ARRAY PLACEMENT
int statArray[9];
statArray[0] = health;
statArray[1] = magicDamage;
statArray[2] = magicResist;
statArray[3] = physicalResist;
statArray[4] = physicalDamage;
statArray[5] = magicOffMastery;
statArray[6] = physicalOffMastery;
statArray[7] = magicDefMastery;
statArray[8] = physicalDefMastery;
std::string stats[9];
stats[0] = "Health : " ;
stats[1] = "Magic Damage : " ;
stats[2] = "Magic Resist : " ;
stats[3] = "Physical Resist : " ;
stats[4] = "Physical Damage : " ;
stats[5] = "Magic Offensive Mastery : " ;
stats[6] = "Physical Offensive Mastery : ";
stats[7] = "Magic Defensive Mastery : " ;
stats[8] = "Physical Defensive Mastery : ";
int statString = 0;
int statInt = 0;
while (statPoints > 0)
{
std::cout << "*******************************************************************************" << std::endl;
std::cout << "* *" << std::endl;
std::cout << "* CHARACTER CREATION *" << std::endl;
std::cout << "* *" << std::endl;
std::cout << "*******************************************************************************" << std::endl;
std::cout << " Health : " << health * 10 << std::endl;
std::cout << std::endl;
std::cout << " Magic Damage : " << magicDamage << std::endl;
std::cout << std::endl;
std::cout << " Magic Resist : " << magicResist << std::endl;
std::cout << std::endl;
std::cout << " Physical Resist : " << physicalResist << std::endl;
std::cout << std::endl;
std::cout << " Physical Damage : " << physicalDamage << std::endl;
std::cout << std::endl;
std::cout << " Magic Offensive Mastery : " << magicOffMastery << std::endl;
std::cout << std::endl;
std::cout << " Physical Offensive Mastery : " << physicalOffMastery << std::endl;
std::cout << std::endl;
std::cout << " Magic Defensive Mastery : " << magicDefMastery << std::endl;
std::cout << std::endl;
std::cout << " Physical Defensive Mastery : " << physicalDefMastery << std::endl;
std::cout << "*******************************************************************************" << std::endl;
std::cout << "STAT POINTS: " << statPoints << std::endl;
std::cout << stats[statString] ;
std::cin >> statArray[statInt] ;
statPoints -= statArray[statInt];
++statString;
++statInt;
}
}
As you might notice, I'm trying to have the user change the value of health, which is stored in statArray[statInt], which equates to statArray[0], then I ++statInt. The idea was I'd be able to have the user input all his stats one at a time. Instead of my intention, whats happening is it's taking the user input as a reference to the array slot. (statArray[0], statArray[1]) etc. Instead of the stat associated with that array slot.
To achieve what you want to do you could use a union:
union Stat {
struct {
int health;
int magicDamage;
int magicResist;
// ...
};
int array[3];
};
int main() {
Stat stat;
stat.array[1] = 42;
cout<<stat.magicDamage<<endl; // should give 42
};
However a better solution would be to use a map:
map<string,int> stat;
const char *keys[] = {"health","magicDamage","magicResist"};
int main() {
for (int i=0;i<3; ++i) {
cout<<"enter "<<keys[i]<<endl;
cin>>stat[keys[i]];
}
}
Change statArraydefinition to use pointers instead of values
int *statArray[9];
statArray[0] = &health;
statArray[1] = &magicDamage;
statArray[2] = &magicResist;
statArray[3] = &physicalResist;
statArray[4] = &physicalDamage;
statArray[5] = &magicOffMastery;
statArray[6] = &physicalOffMastery;
statArray[7] = &magicDefMastery;
statArray[8] = &physicalDefMastery;
And update next lines:
std::cout << *stats[statString] ;
std::cin >> *statArray[statInt] ;
statPoints -= *statArray[statInt];
I have a problem with the class in C++; it's written like this.
in second source,
void idealtype::compare(idealtype T1)
{
if (height.size() > T1.height.size())
cout << T1.getname() << " " << T1.getage() << "\t" << T1.getheight() << "\n";
else if (height.size() < T1.height.size())
cout << getname() << " " << getage() << "\t" << getheight() << "\n";
else if (height.size() == T1.height.size())
{
cout << T1.getname() << " " << T1.getage() << "\t" << T1.getheight() << "\n";
cout << getname() << " " << getage() << "\t" << getheight() << "\n";
}
cout << "\n";
}
in header;
class idealtype
{public:
void compare(idealtype);
....
private:
int height;
}
in main source;
....
idealtype A(a,b,c) // c is "height"
....
idealtype B(a,b,c) // c is "height"
B.compare(A)
I think it's all done well, but Visual keeps showing me,
(in second source, on every if() state) error : expression must have class type
So, what's the KEY of this problem?
Plz help me, guys :)
So, what's the KEY of this problem?
In your code you say
if (height.size() > T1.height.size())
class idealtype {
// ...
private:
int height; // <<<<<<<<<<<<
};
since height is declared as int it doesn't have any class like methods. That's why the compiler complains.