// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
# include <iostream>
using namespace std;
// a class for complex numbers
template <class T>
class cvect
{
public:
// Constructors
cvect();
cvect(T,T);
cvect(cvect<T>&);
//Overlaoding operators
cvect<T> operator+(cvect<T>);
cvect<T>& operator= (const cvect<T>&);
void disp();
private:
T real;
T image;
};
template <class T>
cvect<T>::cvect()
{
real=0;
image=0;
}
template <class T>
cvect<T>::cvect(T r, T i)
{
real=r;
image=i;
}
template <class T>
cvect<T>::cvect(cvect<T>& arg)
{
real=arg.real;
image=arg.image;
}
template <class T>
cvect<T> cvect<T>::operator+(cvect<T> param) {
cvect<T> temp;
temp.real = real + param.real;
temp.image = image + param.image;
return (temp);
}
template <class T>
cvect<T>& cvect<T>::operator= (const cvect<T>& arg)
{
real=arg.real;
image=arg.image;
return *this;
}
template <class T>
void cvect<T>::disp()
{
cout<<real<<" + i"<<image<<endl;
}
int main()
{
cvect<int> a;
cvect<int> b(3,4);
cvect<int> c=b;
a = c + b;
b.disp();
b.disp();
a.disp();
cvect<double> a1;
cvect<double> b1(3.12,4.18);
cvect<double> c1=b1;
a1 = c1 + b1;
b1.disp();
b1.disp();
a1.disp();
getchar();
return 0;
}
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
# include <iostream>
using namespace std;
// a class for complex numbers
template <class T>
class cvect
{
public:
// Constructors
cvect();
cvect(T,T);
cvect(cvect<T>&);
//Overlaoding operators
cvect<T> operator+(cvect<T>);
cvect<T>& operator= (const cvect<T>&);
void disp();
private:
T real;
T image;
};
template <class T>
cvect<T>::cvect()
{
real=0;
image=0;
}
template <class T>
cvect<T>::cvect(T r, T i)
{
real=r;
image=i;
}
template <class T>
cvect<T>::cvect(cvect<T>& arg)
{
real=arg.real;
image=arg.image;
}
template <class T>
cvect<T> cvect<T>::operator+(cvect<T> param) {
cvect<T> temp;
temp.real = real + param.real;
temp.image = image + param.image;
return (temp);
}
template <class T>
cvect<T>& cvect<T>::operator= (const cvect<T>& arg)
{
real=arg.real;
image=arg.image;
return *this;
}
template <class T>
void cvect<T>::disp()
{
cout<<real<<" + i"<<image<<endl;
}
int main()
{
cvect<int> a;
cvect<int> b(3,4);
cvect<int> c=b;
a = c + b;
b.disp();
c.disp();
a.disp();
cvect<double> a1;
cvect<double> b1(3.12,4.18);
cvect<double> c1=b1;
a1 = c1 + b1;
c1.disp();
b1.disp();
a1.disp();
getchar();
return 0;
}
without this constructor, I can compile the code and run it.
1>------ Rebuild All started: Project: Complex, Configuration: Debug Win32 ------
1> complexOperator.cpp
1>..\complex\complexoperator.cpp(76): error C2440: 'argument' : cannot convert from 'cvect<T>' to 'cvect<T> &'
1> with
1> [
1> T=int
1> ]
1> A non-const reference may only be bound to an lvalue
1> while checking that elided copy-constructor 'cvect<T>::cvect(cvect<T> &)' is callable
1> with
1> [
1> T=int
1> ]
1> ..\complex\complexoperator.cpp(15) : see declaration of 'cvect<T>::cvect'
1> with
1> [
1> T=int
1> ]
1> when converting from 'cvect<T>' to 'const cvect<T> &'
1> with
1> [
1> T=int
1> ]
1>..\complex\complexoperator.cpp(85): error C2440: 'argument' : cannot convert from 'cvect<T>' to 'cvect<T> &'
1> with
1> [
1> T=double
1> ]
1> A non-const reference may only be bound to an lvalue
1> while checking that elided copy-constructor 'cvect<T>::cvect(cvect<T> &)' is callable
1> with
1> [
1> T=double
1> ]
1> ..\complex\complexoperator.cpp(15) : see declaration of 'cvect<T>::cvect'
1> with
1> [
1> T=double
1> ]
1> when converting from 'cvect<T>' to 'const cvect<T> &'
1> with
1> [
1> T=double
1> ]
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
Title | # Comments | Views | Activity |
---|---|---|---|
Fast, free, private online code repository / editor | 7 | 82 | |
Should CArray be used for a list of pointers in C++? | 19 | 90 | |
C++ error cannot convert from std::string to const char * | 6 | 91 | |
Embarcadero WebBroker REST server endpoint parameters | 2 | 19 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
13 Experts available now in Live!