I have an error in my code, I want to display the sume of 2 objects with pointers in a class. Please help me to fix it, maybe is due to the pointers. Can you see what's wrong?
This is the error:
<source>(79): error C2280: 'Pair &Pair::operator =(const Pair &)': attempting to reference a deleted function
<source>(60): note: compiler has generated 'Pair::operator =' here
<source>(60): note: 'Pair &Pair::operator =(const Pair &)': function was implicitly deleted because 'Pair' has a user-defined move constructor
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Pair {
int *x, *y;
public:
Pair() {
x = new int(sizeof(x));
y = new int(sizeof(y));
*x = 0;
*y = 0;
}
Pair(int a, int b) {
x = new int(sizeof(x));
y = new int(sizeof(y));
*x = a;
*y = b;
}
Pair(Pair& ob) {
x = new int(sizeof(ob.x));
y = new int(sizeof(ob.y));
*x = *(ob.x);
*y = *(ob.y);
}
Pair(Pair&& ob) {
x = new int(sizeof(ob.x));
y = new int(sizeof(ob.y));
*x = *(ob.x);
*y = *(ob.y);
}
Pair(int a):Pair(a, 0) {
}
void setX(int X) {
*x = X;
}
void setY(int Y) {
*y = Y;
}
int* getX() {
return x;
}
int* getY() {
return y;
}
~Pair() {
delete[]x;
delete[]y;
}
Pair sume(Pair ob1){
Pair ob2;
*(ob2.x) = *(ob1.x) + (*x);
*(ob2.y) = *(ob1.y) + (*y);
return ob2;
}
double media() {
return (double(*x) + double(*y)) / 2;
}
};
int main() {
Pair ob1, ob2(5), ob3(4, 3);
ob1.setX(6);
ob1.setY(7);
cout << "X= " << *(ob1.getX())<<endl;
cout << "Y= " << *(ob1.getY())<<endl;
cout << "Media este: " << ob1.media();
cout << "\nX= " << *(ob2.getX()) << endl;
cout << "Y= " << *ob2.getY() << endl;
cout << "Media este: " << ob2.media();
cout << "\nX= " << *(ob3.getX()) << endl;
cout << "Y= " << *(ob3.getY()) << endl;
cout << "Media este: " << ob3.media();
Pair ob4,ob5,ob6;
ob4 = ob1.sume(ob2);//here the compiler shows the error
cout <<"\nX= "<< *(ob4.getX())<<endl;
cout << "Y= " << *(ob4.getY())<<endl;
}
#include <iostream>
struct Vector {
float x, y, z;
};
int main() {
size_t offset = (long)&((Vector*)nullptr)->x;
std::cout << offset << std::endl;
std::cout << (int)offsetof(struct Vector,y) << std::endl;
std::cout << (int)offsetof(struct Vector,z) << std::endl;
return 0;
}
What does offset of a struct member exactly means and what is its purpose?
I have tried with some online resources and I could get it.Off
Explain how offset exaclty works with this code!
my english not good ,sorry.
clion class variable not initialized why appear z =16
and vs2019 situation not same
#include <iostream>
using namespace std;
class bas {
public:
int showget();
private:
int x, z, y;
};
int main() {
bas B;
B.showget();
return 0;
}
int bas::showget() {
cout << x << " " << " " << z << " " << y << "\n";
int *t;
t = &x;
cout << t<<"\n";
t = &y;
cout << t<< "\n";
t = &z;
cout << t;
return 0;
}
}
}
enter image description here
I am doing a program to test swapping couple of things by reference.
I managed to get the first two functions in my code to work but can't get to change the char * in the third function.
I think the problem is that it's a constant and only valid to read-only
that's what the error is telling me but How to be able to work with it in this way?
Here is the code:
#include <iostream>
using namespace std;
void swapping(int &x, int &y)
{
int temp =x;
x=y;
y=temp;
}
void swapping(float &x, float &y)
{
float temp=x;
x=y;
y=temp;
}
void swapping(const char *&x,const char *&y)
{
int help = *x;
(*x)=(*y);
(*y)=help;
} // swap char pointers
int main(void) {
int a = 7, b = 15;
float x = 3.5, y = 9.2;
const char *str1 = "One";
const char *str2 = "Two";
cout << "a=" << a << ", b=" << b << endl;
cout << "x=" << x << ", y=" << y << endl;
cout << "str1=" << str1 << ", str2=" << str2 << endl;
swapping(a, b);
swapping(x, y);
swapping(str1, str2);
cout << "\n";
cout << "a=" << a << ", b=" << b << endl;
cout << "x=" << x << ", y=" << y << endl;
cout << "str1=" << str1 << ", str2=" << str2 << endl;
return 0;
}
As suggested in the comments:
void swapping(const char*& x, const char*& y)
{
auto t = x;
x = y;
y = t;
}
Now you should consider to use a template:
template<typename Type>
void swapping(Type& a, Type& b)
{
auto t = a;
a = b;
b = t;
}
I am trying to make converting between a custom class and OpenCV easier.
Converting constructors allow us to declare a class without the explicit keyword and construct from an alternate data type.
For example, we could declare two classes:
class Foo
{
public:
std::vector<int32_t> data;
Foo(int k) : data(k) {}
};
class FooProxy : public cv::Mat
{
public:
FooProxy(const Foo & foo) : cv::Mat(foo.data.size(), 1, CV_32SC1, (void*)foo.data.data()) {}
};
And then we can create a FooProxy by setting it equal and then then using any OpenCV function we want:
Foo myFoo(3);
myFoo.data = { 1,2,3 };
FooProxy fp = myFoo;
fp.setTo(1);
float myNorm = cv::norm(fp);
std::cout << "myFoo: " << myFoo.data.at(0) << ", " << myFoo.data.at(1) << ", " << myFoo.data.at(2) << "\n";
std::cout << "fp: " << fp.at<int32_t>(0) << ", " << fp.at<int32_t>(1) << ", " << fp.at<int32_t>(2) << "\n";
std::cout << "The Norm is: " << myNorm << "\n";
with output:
myFoo: 1, 1, 1
fp: 1, 1, 1
The Norm is: 1.73205
However, I would much rather write:
float myNorm = cv::norm(myFoo);
and have c++ automatically convert myFoo. Is this possible?
This works:
float myNorm2 = cv::norm(FooProxy(myFoo));
but not the simpler version.
You could create a conversion operator
class Foo
{
...
operator cv::Mat()
{
return cv::Mat(data.size(), 1, CV_32SC1, (void*)data.data());
}
};
Edit cv::norm has the following overloads (notice no cv::Mat):
cv::norm(const Matx< _Tp, m, n > &M)
cv::norm(const Matx< _Tp, m, n > &M, int normType)
cv::norm(InputArray src1, int normType=NORM_L2, InputArray mask=noArray()) // this is the one we want
cv::norm(InputArray src1, InputArray src2, int normType=NORM_L2, InputArray mask=noArray())
cv::norm(const SparseMat &src, int normType)
A cv::InputArray can be constructed from a cv::Mat, however you're only allowed 1 user-defined conversion implicitly. So you'll actually need a conversion operator for cv::InputArray if you want Foo to work with cv::norm.
Then you should define a function in namespace cv like this:
namespace cv{
float norm(const Foo& foo){
return cv::norm(FooProxy(foo));
}
}
Now you can call:
float res = cv::norm(myFoo);
The code:
//! 2018.01.17 09:42:16 CST
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
class Foo
{
public:
std::vector<int32_t> data;
Foo(int k) : data(k) {}
};
class FooProxy : public cv::Mat
{
public:
FooProxy(const Foo & foo) : cv::Mat(foo.data.size(), 1, CV_32SC1, (void*)foo.data.data()) {}
};
namespace cv {
float norm(const Foo& foo) {
return cv::norm(FooProxy(foo));
}
}
int main() {
Foo myFoo(3);
myFoo.data = { 1,2,3 };
FooProxy fp = myFoo;
fp.setTo(1);
float myNorm = cv::norm(fp);
std::cout << "myFoo: " << myFoo.data.at(0) << ", " << myFoo.data.at(1) << ", " << myFoo.data.at(2) << "\n";
std::cout << "fp: " << fp.at<int32_t>(0) << ", " << fp.at<int32_t>(1) << ", " << fp.at<int32_t>(2) << "\n";
std::cout << "The Norm is: " << myNorm << "\n";
float myNorm2 = cv::norm(FooProxy(myFoo));
std::cout << "The Norm2 is:"<<myNorm2<<endl;
float myNorm3 = cv::norm(myFoo);
std::cout << "The Norm3 is:"<<myNorm2<<endl;
}
The result:
myFoo: 1, 1, 1
fp: 1, 1, 1
The Norm is: 1.73205
The Norm2 is:1.73205
The Norm3 is:1.73205