// 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 ==========
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
Template syntax for variable length arrays | 9 | 71 | |
C++ Class Serialization, Encapsulation and Formatted Output | 8 | 53 | |
Unresolved External Symbols | 3 | 68 | |
C++ mouse_event mouse look | 7 | 65 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
14 Experts available now in Live!