Creating template vector. stream and other error.

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 837 Views - Last Post: 20 June 2012 - 07:19 AM Rate Topic: -----

#1 leeto  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 38
  • Joined: 17-June 12

Creating template vector. stream and other error.

Posted 17 June 2012 - 05:31 AM

This is study project because of this I don't use code from STL vector.
I want to create some program that will be do next:
1. Create object of class for example Point (class that has x,y coordinate)
2. Create object of <template> name "Array" with fix amount of elements.
3. Put object of class Point to Array on needed me place that represent index of Array. Using fucntion SetElement
4. Using function GetElement I want to this this "Point" object on screen.
Very simple, please help !!! What I did wrong ???

Please I really just want to see correction of code even without comments...
Thanks in advance


// Array.h
// Templated Array class containging Ts
#include <sstream>
#include <iostream>

#ifndef Array_H
#define Array_H
 
template <class Type=double> class Array
{
private:
int m_size; 
Type* m_data;
 
public:
Array();
Array(int new_size); 
void SetElement(Type& type_object, int index); 
const Type& GetElement(int index) const; 
};
 

#ifndef Array_cpp 
#include "array.cpp"
#endif
 
#endif

//*********************

//array.cpp
#include "array.h"
#include <sstream>
#include <iostream>
using namespace std;
#ifndef Array_CPP
#define Array_CPP

template <class Type>
Array<Type>::Array()                                     
{
    m_size = 10;
     m_data = new Type[m_size];
}

template <class Type> 
Array<Type>::Array(int new_size)					
{
    new_size = m_size;
    m_data = new Type[m_size];
}


template <class Type> 
void Array<Type>::SetElement(Type& type_object, int index)
{
	try
	{
		if (index >= m_size){throw 11;} 
		m_data[index] = type_object;
		cout << "Set Element " << type_object  << endl;
	}
	 catch (int x )
	 {
		cout << "ERROR" << x << " ignore the set, because index of element bigger then massive size"<< endl;
	 }
}
template <class Type> 
const Type& Array<Type>::GetElement(int index) const
{
	try 
	{
		if (index > m_size || index < 0){ throw 1;} 
	} 
	catch (double x)
	{
	cout << "index incorrect, index too big or too small " << x << endl; 
	}  
	return m_data[index];
}
#endif Array_CPP

//********************

		//main.cpp
		#include <iostream>
		#include <sstream>
		#include "array.h"
		using namespace std;

	class Point
{
private:
    double m_x;                                
    double m_y;                                
public:
    // Constructors
    Point(): m_x(0), m_y(0) {};                            
	Point(double new_x, double new_y) : m_x(new_x), m_y(new_y) {};
};


		int main()
		{
			Point *p1 = new Point (1,12);
				cout << endl; 
			Array<Point> arr1[1];
			arr1[1].SetElement(*p1,0);
				cout << endl;
			arr1[1].GetElement(0) ;	
			delete p1;
			return 0;
		}
 



Compiler errors give a lot of words related with streams or something for examlple :
_Traits=std::char_traits<char>
1> ]
1> c:\Program Files\Microsoft Visual Studio 10.0\VC\include\ostream(467): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(const void *)'
1> with

1>------ Build started: Project: HP_4.2a, Configuration: Release Win32 ------
1> TestArray.cpp
1>c:\all my\с++\ha level 6\solution\level 6\hp_4.2a\array.cpp(31): error C2679:
1> TestArray.cpp(23) : see reference to class template instantiation

PS I create object of "Point " dynamically because of condition of study project.

Is This A Good Question/Topic? 0
  • +

Replies To: Creating template vector. stream and other error.

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,346
  • Joined: 25-December 09

Re: Creating template vector. stream and other error.

Posted 17 June 2012 - 05:42 AM

Post your complete error message, exactly as it appears in your development environment. These messages have important information embedded within them to aid in location and repair of the reported error.

Also with templates both the definition and the implementation must be in the same compilation unit. I don't recommend including a .cpp file, even for templates. In my opinion you should rename your included .cpp file to something other than a .cpp, maybe something like .inc, to indicate that this is not a file that can be compiled by it's self. Next you are conditionally including the .cpp file in your header. You will need to include this file every time this header is included, so remove the guards.

Jim

This post has been edited by jimblumberg: 17 June 2012 - 05:48 AM

Was This Post Helpful? 1
  • +
  • -

#3 leeto  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 38
  • Joined: 17-June 12

Re: Creating template vector. stream and other error.

Posted 17 June 2012 - 06:20 AM

View Postjimblumberg, on 17 June 2012 - 05:42 AM, said:

Post your complete error message, exactly as it appears in your development environment. These messages have important information embedded within them to aid in location and repair of the reported error.

Also with templates both the definition and the implementation must be in the same compilation unit. I don't recommend including a .cpp file, even for templates. In my opinion you should rename your included .cpp file to something other than a .cpp, maybe something like .inc, to indicate that this is not a file that can be compiled by it's self. Next you are conditionally including the .cpp file in your header. You will need to include this file every time this header is included, so remove the guards.

Jim


Now I done some correction and my whole compiler error is:
1>------ Build started: Project: 42a, Configuration: Debug Win32 ------
1> Skipping... (no relevant changes detected)
1> Array.cpp
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Array<class Point>::~Array<class Point>(void)" (??1?$Array@VPoint@@@@QAE@XZ) referenced in function _main
1>C:\all my\с++\HA level 6\Solution1\Level6\Debug\42a.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

but compiler open to me console window where I can see the next message... This message on my native language I try to translate... main idea is that it refers me to folder from place my .exe file that tell that this .exe " are not an internal or external command, used by the program or batch file."
// Array.h
// Templated Array class containging Ts
#include <sstream>
#include <iostream>

#ifndef Array_H
#define Array_H
 
template <class Type> class Array
{
private:
Type* m_data;
int m_size; 
 
public:
Array();
Array(int new_size); 
void SetElement(Type& type_object, int index); 
const Type& GetElement(int index) const; 
~Array();
};
 

#ifndef Array_cpp 
#include "array.cpp"
#endif
 
#endif

//array.cpp
#include "array.h"
#include <sstream>
#include <iostream>
using namespace std;
#ifndef Array_CPP
#define Array_CPP

template <class Type>
Array<Type>::Array()                                     
{
    m_size = 10;
     m_data = new Type[m_size];
}

template <class Type> 
Array<Type>::Array(int new_size)					
{
    new_size = m_size;
    m_data = new Type[m_size];
}


template <class Type> 
void Array<Type>::SetElement(Type& type_object, int index)
{
	try
	{
		if (index >= m_size){throw 11;} 
		m_data[index] = type_object;
		cout << "Set Element " << type_object  << endl;
	}
	 catch (int x )
	 {
		cout << "ERROR" << x << " ignore the set, because index of element bigger then massive size"<< endl;
	 }
}
template <class Type> 
const Type& Array<Type>::GetElement(int index) const
{
	try 
	{
		if (index > m_size || index < 0){ throw 1;} 
	} 
	catch (double x)
	{
	cout << "index incorrect, index too big or too small " << x << endl; 
	}  
	return m_data[index];
}
#endif //Array_CPP

		//main.cpp
		#include <iostream>
		#include <sstream>
		#include "array.h"
		using namespace std;

	class Point
{
private:
    double m_x;                                
    double m_y;                                
public:
    // Constructors
    Point(): m_x(0), m_y(0) {};                            
	Point(double new_x, double new_y) : m_x(new_x), m_y(new_y) {};
	friend ostream& operator << (ostream& os, const Point& point)
{
    return os << point.ToString();
}
	std::string Point::ToString(void) const                // create a string representation of a point
{
// create a string like: “Point(1.5, 3.9)”
      std::ostringstream os;
    os << m_x << " , " << m_y;
    std::string double_string = os.str();
 
    return "Point(" + double_string + ")";
}
};


		int main()
		{
			Point *p1 = new Point (1,12);
				cout << endl; 
			Array<Point> arr1[1];
			arr1[1].SetElement(*p1,0);
				cout << endl;
			arr1[1].GetElement(0) ;	
			delete p1;
			return 0;
		}
 

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,346
  • Joined: 25-December 09

Re: Creating template vector. stream and other error.

Posted 17 June 2012 - 06:30 AM

Quote

1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Array<class Point>::~Array<class Point>(void)"

This is a linker error, telling you that the linker can't find the stated function or variable.

In your Array class you have defined a destructor, but I don't see the actual implementation.

template <class Type> class Array
{
.....
~Array(); // Where do you implement this function???
};




Jim

This post has been edited by jimblumberg: 17 June 2012 - 06:32 AM

Was This Post Helpful? 1
  • +
  • -

#5 leeto  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 38
  • Joined: 17-June 12

Re: Creating template vector. stream and other error.

Posted 17 June 2012 - 07:18 AM

View Postjimblumberg, on 17 June 2012 - 06:30 AM, said:

Quote

1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Array<class Point>::~Array<class Point>(void)"

This is a linker error, telling you that the linker can't find the stated function or variable.

In your Array class you have defined a destructor, but I don't see the actual implementation.

template <class Type> class Array
{
.....
~Array(); // Where do you implement this function???
};




Jim

if I us this code : they give me exceptional handling message even if I put arr1[1].SetElement(*p1,0) zero index... didn't any other reaction
also give me system error see in atachment
		int main()
		{
			Point *p1 = new Point (1,12);
				cout << endl; 
			Array<Point> arr1[1];
			arr1[1].SetElement(*p1,1);
				cout << endl;
			arr1[1].GetElement(1) ;	
			delete p1;
			return 0;
		}





if I use this variant they give me the say error

		int main()
		{
			Point *p1 = new Point (1,12);
				cout << endl; 
			Array<Point> arr1(1);// not [] but ()
			arr1.SetElement(*p1,1); // arr1   without  []
				cout << endl;
			arr1.GetElement(1) ;	// arr1   without  []
			delete p1;
			return 0;
		}
 


BTW compiler say :
1>------ Build started: Project: 42a, Configuration: Debug Win32 ------
1>LINK : fatal error LNK1168: cannot open C:\all my\с++\HA level 6\Solution1\Level6\Debug\42a.exe for writing
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,346
  • Joined: 25-December 09

Re: Creating template vector. stream and other error.

Posted 17 June 2012 - 07:25 AM

Post your current code, along with an error messages you received when you compiled your program.

Jim
Was This Post Helpful? 1
  • +
  • -

#7 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,240
  • Joined: 31-December 10

Re: Creating template vector. stream and other error.

Posted 17 June 2012 - 07:29 AM

It's not very helpful to throw just an integer value. To be more descriptive, you can either derive a class from the std::exception class, or just declare an empty class with a descriptive name like:
class ArrayFull {};
class IndexOutOfRange {};

Then you can have code like this which is more helpful IMO:
if(index > MAX) throw ArrayFull;
// ...
try
{
    //...
}
catch(ArrayFull)
{
   cerr << "Error: \"array is full\"\n";
}



You should have a look at these reference pages for the "exception" and "stdexcept" headers: std::exception and stdexcept

This post has been edited by vividexstance: 17 June 2012 - 07:30 AM

Was This Post Helpful? 1
  • +
  • -

#8 leeto  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 38
  • Joined: 17-June 12

Re: Creating template vector. stream and other error.

Posted 17 June 2012 - 07:32 AM

View Postjimblumberg, on 17 June 2012 - 07:25 AM, said:

Post your current code, along with an error messages you received when you compiled your program.

Jim



// Array.h
// Templated Array class containging Ts
#include <sstream>
#include <iostream>

#ifndef Array_H
#define Array_H
 
template <class Type> class Array
{
private:
Type* m_data;
int m_size; 
 
public:
Array();
Array(int new_size); 
void SetElement(Type& type_object, int index); 
const Type& GetElement(int index) const; 
~Array(){};
};
 

#ifndef Array_cpp 
#include "array.cpp"
#endif
 
#endif

//array.cpp
#include "array.h"
#include <sstream>
#include <iostream>
using namespace std;
#ifndef Array_CPP
#define Array_CPP

template <class Type>
Array<Type>::Array()                                     
{
    m_size = 10;
     m_data = new Type[m_size];
}

template <class Type> 
Array<Type>::Array(int new_size)					
{
    new_size = m_size;
    m_data = new Type[m_size];
}


template <class Type> 
void Array<Type>::SetElement(Type& type_object, int index)
{
	try
	{
		if (index >= m_size){throw 11;} 
		m_data[index] = type_object;
		cout << "Set Element " << type_object  << endl;
	}
	 catch (int x )
	 {
		cout << "ERROR" << x << " ignore the set, because index of element bigger then massive size"<< endl;
	 }
}
template <class Type> 
const Type& Array<Type>::GetElement(int index) const
{
	try 
	{
		if (index > m_size || index < 0){ throw 1;} 
	} 
	catch (double x)
	{
	cout << "index incorrect, index too big or too small " << x << endl; 
	}  
	return m_data[index];
}
#endif //Array_CPP

		//main.cpp
		#include <iostream>
		#include <sstream>
		#include "array.h"
		using namespace std;

	class Point
{
private:
    double m_x;                                
    double m_y;                                
public:
    // Constructors
    Point(): m_x(0), m_y(0) {};                            
	Point(double new_x, double new_y) : m_x(new_x), m_y(new_y) {};
	friend ostream& operator << (ostream& os, const Point& point)
{
    return os << point.ToString();
}
	std::string Point::ToString(void) const                // create a string representation of a point
{
// create a string like: “Point(1.5, 3.9)”
      std::ostringstream os;
    os << m_x << " , " << m_y;
    std::string double_string = os.str();
 
    return "Point(" + double_string + ")";
}
};


		int main()
		{
			Point *p1 = new Point (1,12);
				cout << endl; 
			Array<Point> arr1(1);
			arr1.SetElement(*p1,1);
				cout << endl;
			arr1.GetElement(1) ;	
			delete p1;
			return 0;
		}
 




'42a.exe': Loaded 'C:\all my\с++\HA level 6\Solution1\Level6\Debug\42a.exe', Symbols loaded.
'42a.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', Cannot find or open the PDB file
'42a.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', Cannot find or open the PDB file
'42a.exe': Loaded 'C:\WINDOWS\system32\msvcp100d.dll', Symbols loaded.
'42a.exe': Loaded 'C:\WINDOWS\system32\msvcr100d.dll', Symbols loaded.
'42a.exe': Loaded 'C:\WINDOWS\system32\user32.dll', Cannot find or open the PDB file
'42a.exe': Loaded 'C:\WINDOWS\system32\gdi32.dll', Cannot find or open the PDB file
'42a.exe': Loaded 'C:\WINDOWS\system32\imm32.dll', Cannot find or open the PDB file
'42a.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll', Cannot find or open the PDB file
'42a.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll', Cannot find or open the PDB file
'42a.exe': Loaded 'C:\WINDOWS\system32\secur32.dll', Cannot find or open the PDB file
'42a.exe': Loaded 'C:\WINDOWS\system32\uxtheme.dll', Cannot find or open the PDB file
'42a.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll', Cannot find or open the PDB file
'42a.exe': Loaded 'C:\Program Files\Yandex\Punto Switcher\pshook.dll', Binary was not built with debug information.
'42a.exe': Loaded 'C:\WINDOWS\system32\shlwapi.dll', Cannot find or open the PDB file
'42a.exe': Loaded 'C:\WINDOWS\system32\imagehlp.dll', Cannot find or open the PDB file
'42a.exe': Loaded 'C:\WINDOWS\system32\msctf.dll', Cannot find or open the PDB file
'42a.exe': Loaded 'C:\Documents and Settings\All Users\Application Data\Real\RealPlayer\BrowserRecordPlugin\Chrome\Hook\rpchrome14browserrecordhelper.dll', Cannot find or open the PDB file
'42a.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.6161_x-ww_31a54e43\msvcr90.dll', Cannot find or open the PDB file
'42a.exe': Loaded 'C:\WINDOWS\system32\shell32.dll', Cannot find or open the PDB file
'42a.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.6161_x-ww_31a54e43\msvcp90.dll', Cannot find or open the PDB file
'42a.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.6028_x-ww_61e65202\comctl32.dll', Cannot find or open the PDB file
'42a.exe': Loaded 'C:\WINDOWS\system32\comctl32.dll', Cannot find or open the PDB file
'42a.exe': Loaded 'C:\WINDOWS\system32\version.dll', Cannot find or open the PDB file
'42a.exe': Unloaded 'C:\WINDOWS\system32\version.dll'
'42a.exe': Loaded 'C:\WINDOWS\system32\msctfime.ime', Cannot find or open the PDB file
'42a.exe': Loaded 'C:\WINDOWS\system32\ole32.dll', Cannot find or open the PDB file

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#9 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,240
  • Joined: 31-December 10

Re: Creating template vector. stream and other error.

Posted 17 June 2012 - 08:02 AM

My suggestion would be to take a step back first of all and define the Array class as just a regular class first. Have it hold one of the built-in data types like int or char. Once you have a working class, rigorously test it too make sure there are no bugs. Once you feel you have tested it thoroughly, then you should change the Array class to a template and go from there.

For most compilers, when dealing with templates, any error messages you receive are going to be mostly long and seemingly unhelpful. That's why it's recommended to first write a regular class and test it, then change it to a template version.

*EDIT*: Do what jimblumberg suggested in post #4. You declare a destructor for your Array class but you never define it.

This post has been edited by vividexstance: 17 June 2012 - 08:05 AM

Was This Post Helpful? 1
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,346
  • Joined: 25-December 09

Re: Creating template vector. stream and other error.

Posted 17 June 2012 - 08:37 AM

The only problem I see in your latest code is the following line:
			Array<Point> arr1(1);


You have not defined an Array constructor that takes one argument. You have zero or two argument constructors. Also you Array destructor should be deleting the memory your class creates with new.

All those warnings that contain "PDB" are because your compiler can't find the "Debug" system dlls. I am not familiar with Visual C++ so I don't really know how to get rid of those annoying error messages the proper way. But one way is to compile a "release" mode program.


Jim

This post has been edited by jimblumberg: 17 June 2012 - 08:37 AM

Was This Post Helpful? 1
  • +
  • -

#11 leeto  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 38
  • Joined: 17-June 12

Re: Creating template vector. stream and other error.

Posted 17 June 2012 - 11:27 PM

View Postjimblumberg, on 17 June 2012 - 08:37 AM, said:

The only problem I see in your latest code is the following line:
			Array<Point> arr1(1);


You have not defined an Array constructor that takes one argument. You have zero or two argument constructors. Also you Array destructor should be deleting the memory your class creates with new.

All those warnings that contain "PDB" are because your compiler can't find the "Debug" system dlls. I am not familiar with Visual C++ so I don't really know how to get rid of those annoying error messages the proper way. But one way is to compile a "release" mode program.


Jim



I have constructor that contain 1 argument

template <class Type> 
Array<Type>::Array(int new_size)					
{
    new_size = m_size;
    m_data = new Type[m_size];
}

Was This Post Helpful? 0
  • +
  • -

#12 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,240
  • Joined: 31-December 10

Re: Creating template vector. stream and other error.

Posted 18 June 2012 - 09:59 AM

Your constructor that takes one argument is incorrect:
template <class Type> 
Array<Type>::Array(int new_size)					
{
    [b]new_size = m_size;[/b]
    m_data = new Type[m_size];
}

The private data member that holds the size of the array for the Array class is m_size. The constructor takes a size as an argument but you then set the argument, NOT the data member. Also, m_size contains a garbage value initially so that's what new_size gets set too and it's what is used to allocate the array. If you don't know how to fix this, just switch the left hand side of the assignment with the right hand side on the first line of the constructor.
Was This Post Helpful? 1
  • +
  • -

#13 leeto  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 38
  • Joined: 17-June 12

Re: Creating template vector. stream and other error.

Posted 19 June 2012 - 03:26 AM

I what to implement to my Template operator * .
So, Very simple idea.
There is <Template> Array which purpose is container like vector for classes
There is class Point, each object of contain two coordinate x and y.
So,
1. I wanna fill Array with objects from Point class
2. Multiply each objects from this vector to a factor
3. And print all this bunch of objects ()...

Compile error :
1>------ Build started: Project: HP_4.2b_Ex2, Configuration: Release Win32 ------
1> main.cpp
1>main.cpp(21): error C2440: 'initializing' : cannot convert from 'Point' to 'Array<Type>'
1> with
1> [
1> Type=Point
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

code :
//array.h
#ifndef Array_H
#define Array_H

template <class Type> //Remove the "=double" default parameter.
class Array
{
protected:
  int m_size;
  Type* m_data; //m_data should be a pointer, since you want to allocate data to it

public:
  Array();
  Array(int new_size);
  Array(const Array<Type>& ar);
  ~Array(); //Don't make your destructor virtual. There is no reason to do it.
  Type& operator * (double factor) const;
  Type& operator [] (int index);
  int Size() const;
  void Swap(Array& ar);  
};



#endif

//************
//************

//point.h

#include "array.h"
#include <sstream>
#include <iostream>
using namespace std;


class Point
{
private:
    double m_x;                                
    double m_y;                                
public:
    // Constructors
    Point(): m_x(0), m_y(0) {};                            
	Point(double new_x, double new_y) : m_x(new_x), m_y(new_y) {};
	friend ostream& operator << (ostream& os, const Point& point)
{
    return os << point.ToString();
}
	std::string Point::ToString(void) const                // create a string representation of a point
{
// create a string like: “Point(1.5, 3.9)”
      std::ostringstream os;
    os << m_x << " , " << m_y;
    std::string double_string = os.str();
 
    return "Point(" + double_string + ")";
}
};

//************
//************
//************
//************

//array.cpp
#include "Array.h"
#include <sstream>
#include <iostream>
#include <exception>
using namespace std;
#ifndef Array_CPP
#define Array_CPP


template <class Type>
Array<Type>::Array() : m_size(10), m_data(0) 
{
}
template <class Type>
Array<Type>::Array(int new_size) : m_size(new_size), m_data(new Type[new_size])
{ 
}

template <class Type>
Array<Type>::~Array()
{
  //Technically, the if is not necessary
  if(m_data)
  {
    delete[] m_data;
    m_data = 0;
  }

  //Not necessary either, but just to be clean
  m_size = 0;
}


template <class Type> 
Type& Array<Type>::operator [] (int index) 
{
    cout << "Array [] operator" << endl;
 
    if (index > this->m_size)
	{
        cout << "i am hreeeee" << endl;
        return this->m_data[0];
	}
    return m_data[index];
}
template<class Type>
int Array<Type>::Size() const
{
	return this->m_size; 
}

template<class Type>
Type& Array<Type>::operator * (double factor) const
{
Array<Type> output(Array<Type>::Size());
for(int i=0; i<Array::Size(); i++)
	{
output[i] = Array<T>::operator[](i) * factor;
	}
return output;
}


#endif //Array_CPP

//************
//************
//************
//************

//main.cpp
		#include "point.h"
		
		#include <iostream>
		#include "array.cpp"
		using namespace std;

int main()
{
			  //Create two Point arrays and test the operators
    Array<Point> pArray1(5);
    Array<Point> pArray2(5);
    //initialize
    for(int i=0; i<pArray1.Size(); i++) pArray1[i] = Point(i, i);
    for(int i=0; i<pArray2.Size(); i++) pArray2[i] = Point(2*i, 2*i);
 
    //Numeric Array's operations not working for Point objects
 
 
    cout << "times PointArray1 by 3 and print out the new array: "<< endl;
    Array<Point> answ1 = pArray1 * 3;
	
    for(int i=0; i<answ1.Size(); i++){
        cout << answ1[i] << endl;

    }
 

	
}



Was This Post Helpful? 0
  • +
  • -

#14 leeto  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 38
  • Joined: 17-June 12

Re: Creating template vector. stream and other error.

Posted 19 June 2012 - 03:59 AM

main.cpp(23): Array<Point> answ1 = pArray1 * 3;
under red line: pArray1
and pop -up helper tell that : Error: no suitable user defined conversion from "Point " to Array<Point> exist
Was This Post Helpful? 0
  • +
  • -

#15 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5795
  • View blog
  • Posts: 12,627
  • Joined: 16-October 07

Re: Creating template vector. stream and other error.

Posted 19 June 2012 - 04:26 AM

Overall not bad, but there are several issues.

It looks like you want this Type& operator * (double factor) const; to actually be Array<Type> operator * (double factor) const;.

You don't implement Array(const Array<Type>& ar);. Also, there's a big bug in your empty constructor:
template <class Type>
// WTF?!?
//Array<Type>::Array() : m_size(10), m_data(0) { }
Array<Type>::Array() : m_size(10), m_data(new Type[10]) { }

template <class Type>
Array<Type>::Array(int new_size) : m_size(new_size), m_data(new Type[new_size]) { }

// NULL m_data should never happen!
template <class Type>
Array<Type>::~Array() { delete[] m_data; }



Now, to your immediate issue. You're trying to do "Point * double". You can do this, but first you're going to need to implement an operator overload for point. Something like Point operator * (double factor) const;

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2