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

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

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.

Re: Creating template vector. stream and other error.

Posted 17 June 2012 - 06:20 AM

jimblumberg, 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.

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."

Attached image(s)

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.

Re: Creating template vector. stream and other error.

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.

Re: Creating template vector. stream and other error.

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.

Re: Creating template vector. stream and other error.

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.

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 ()...

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;