![]() |
| | #31 |
| Registered User Join Date: Jan 2008
Posts: 562
| the += is just an operator overloading to add two vectors |
| -EquinoX- is offline | |
| | #32 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,314
| Quote:
Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | ||
| laserlight is online now | |
| | #33 |
| Registered User Join Date: Jan 2008
Posts: 562
| I guess it would add each element that is the vector with each element on the other vector, if the size doesn't match it would throw an exception.. |
| -EquinoX- is offline | |
| | #34 |
| Registered User Join Date: Jan 2008
Posts: 562
| here's the continuation of the instruction, if you wish to know the full set of operators provided below are: Your template class should provide at least the following class or global operator methods: 1 =, ==, !=, +, +=, -, -=, [ ]. Both binary, and unary, operator- should be provided. 2 In operations with scalar type values, the operators *, /, *=, /= can be used. which raises up a new question, how can the element type also have the following operators... Last edited by -EquinoX-; 11-17-2009 at 01:10 AM. |
| -EquinoX- is offline | |
| | #35 |
| Registered User Join Date: Jun 2008
Posts: 1,275
| Don't you want something like Code: vector& operator*=(const vector& other)
{
if (this->size != other.size)
throw std::length_error("No matching size");
for(int i=0; i < this->size; ++i)
this->elements[i] *= other.elements[i];
return *this;
}
Code: vector& operator*=(const vector& other)
{
for(int i=0; i < this->size && i < other.size; ++i)
this->elements[i] *= other.elements[i];
return *this;
}
Last edited by C_ntua; 11-17-2009 at 01:52 AM. |
| C_ntua is offline | |
| | #36 |
| Registered User Join Date: Jan 2008
Posts: 562
| but it only says for operation with scalar values... so how can I know if it's scalar or not, is it directly saying that if I am storing a vector of char or string then I can't use this |
| -EquinoX- is offline | |
| | #37 |
| Registered User Join Date: Jan 2008
Posts: 562
| anyone care to give me some insights? |
| -EquinoX- is offline | |
| | #38 |
| 3735928559 Join Date: Mar 2008
Posts: 693
| Code: template<typename T,size_t N>foo
{
private:
T arr[N];
public:
foo& operator*=(T t)
{
for(size_t i=0;i<N;++i)
{
arr[i]*=t;
}
return *this;
}
};
|
| m37h0d is offline | |
| | #39 |
| Registered User Join Date: Jan 2008
Posts: 562
| what if what's stored in i can't be multiplied by t? what if T is a char or a string? also after giving it some thought, I think type is a class, as the vector is storing an object of type... therefore type is another class... I just don't know how can I make so that type can implement the same operator that vector has, such as *=, etc and I don't know the purpose of type having an int as an argument... if type is a string, then the string takes an int as an argument... but what is that int for in a string? any ideas? Last edited by -EquinoX-; 11-17-2009 at 10:18 PM. |
| -EquinoX- is offline | |
| | #40 | |||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,314
| Quote:
Quote:
Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |||
| laserlight is online now | |
| | #41 |
| 3735928559 Join Date: Mar 2008
Posts: 693
| look at std::valarray. |
| m37h0d is offline | |
| | #42 |
| Registered User Join Date: Jan 2008
Posts: 562
| I think the only way I can restraint it is for the template class vector to take an object type... type can be anything but then I can guarantee that type has all the overload operators |
| -EquinoX- is offline | |
| | #43 | |
| 3735928559 Join Date: Mar 2008
Posts: 693
| no you can't. you're just kicking the can down the road. the point of making that restriction is that your vector class isn't supposed to be used with any type! if you make a Type<std::string> you're going to get a compilation error in Type just the same as you with vector<std::string> if you ever use *= operator of the class. you're still looking at the phrasing of the assignment backwards: Quote:
what that really means is that is the operators that can be used with your vector - it's a requirement of the parameter types, not your vector! you must provide them and use the parameter type's operator, but you are not supposed to try to support an operator that doesn't exist. besides, you'll see that eventually, if you ever intend to use those operators, the underlying paramter type must have them implemented anyway, so your strategy just adds a layer of complication with no gain over the simpler implementation i am suggesting to you. Last edited by m37h0d; 11-18-2009 at 09:29 AM. | |
| m37h0d is offline | |
| | #44 |
| 3735928559 Join Date: Mar 2008
Posts: 693
| Code: #include <valarray>
int _tmain(int argc, _TCHAR* argv[])
{
std::valarray<std::string>thisMakesNoSense;
thisMakesNoSense*="asdf";
return 0;
}
|
| m37h0d is offline | |
| | #45 |
| Registered User Join Date: Jan 2008
Posts: 562
| well it's a requirement for both the vector and the type parameter... I am only confused on how can I make so that whatever type is the parameters then it would support those operators |
| -EquinoX- is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Creating an object of a class inside another class - possible? | Programmer_P | C++ Programming | 34 | 08-15-2009 11:21 PM |
| Need help with a dynamically allocated object inside another class | arya6000 | C++ Programming | 2 | 11-28-2008 05:15 PM |
| Unresolved external on vector inside class constructor | Mario F. | C++ Programming | 13 | 06-20-2006 12:44 PM |
| Declaring an instance of a class inside a class | nickodonnell | C++ Programming | 4 | 10-01-2005 11:46 PM |
| how do i define a const inside a class under private | Unregistered | C++ Programming | 3 | 11-01-2001 06:01 PM |