Sign in to follow this  
lazer_rob

writing values to a matrix class

Recommended Posts

lazer_rob    122
fixed it thanks to the help work like a charm
#ifndef RGBS
#define RGBS

#ifndef RGBvalues
#define RGBvalues
struct RGBvalue {	float r,g,b;   };
#endif


class RGBclass {
public:
	RGBclass();
	RGBclass(float r1,float g1,float b1);
	float r,g,b;
};

inline RGBclass::RGBclass()
{
	r = g = b = 0;
}

inline RGBclass::RGBclass(float r1,float g1,float b1)
{
	r = r1;
	g = g1;
	b = b1;
}

 
  #include <vector>
 
 template<typename T>  // See section on templates for more
 class RGBst {
 public:
	 RGBst();
   RGBst(unsigned nrows, unsigned ncols);
   // Throws a BadSize object if either size is zero
   class BadSize { };
 
   // No need for any of The Big Three!
 
   // Access methods to get the (i,j) element:
   T&       operator() (unsigned i, unsigned j);       // subscript operators often come in pairs
   const T& operator() (unsigned i, unsigned j) const;  // subscript operators often come in pairs
   // These throw a BoundsViolation object if i or j is too big
   class BoundsViolation { };
 
   unsigned nrows() const;  // #rows in this RGBst
   unsigned ncols() const;  // #columns in this RGBst
 
 private:
   std::vector<std::vector<T> > data_;
 };
 
 template<typename T>
 inline unsigned RGBst<T>::nrows() const
 { return data_.size(); }
 
 template<typename T>
 inline unsigned RGBst<T>::ncols() const
 { return data_[0].size(); }
 
 template<typename T>
 inline T& RGBst<T>::operator() (unsigned row, unsigned col)
 {
   if (row >= nrows() || col >= ncols()) throw BoundsViolation();
   return data_[row][col];
 }
 
 template<typename T>
 inline const T& RGBst<T>::operator() (unsigned row, unsigned col) const
 {
   if (row >= nrows() || col >= ncols()) throw BoundsViolation();
   return data_[row][col];
 } 


 template<typename T>
 RGBst<T>::RGBst(unsigned nrows, unsigned ncols)
   : data_ (nrows)
 {
   if (nrows == 0 || ncols == 0)
     throw BadSize();
   for (unsigned i = 0; i < nrows; ++i)
     data_[i].resize(ncols);
 }

 #endif
#include <iostream>
#include "RGBst.h"

int _tmain(int argc, _TCHAR* argv[])
{
	int arrayWidth = 25, arrayHeight = 25;
	RGBvalue a = {222,222,255};
	RGBst<RGBclass> Rst =RGBst<RGBclass>(arrayWidth,arrayHeight); 

	//RGBst  Vbarray;
	//Vbarray = *Rst;
	Rst(10,10)=RGBclass(a.r, a.g, a.b);
	for (int i= 0; i < arrayWidth; i++)
		{
		for (int j= 0; j < arrayHeight; j++)
		{
				Rst(i,j)=RGBclass(a.r, a.g, a.b);
		}
	}
	RGBclass g =RGBclass(Rst(8,8).r,Rst(8,8).g,Rst(8,8).b);
	return 0;
}
[Edited by - lazer_rob on November 28, 2006 12:43:52 PM]

Share this post


Link to post
Share on other sites
Fukushuusha    186
I tried finding the error but I can't find something wrong with it yet.You could help us a little by adding the code in a code segment like that :


#include <iostream>

int main()
{
//blah blah blah
return 0;
}



Read the faq to see how it is done

Share this post


Link to post
Share on other sites
jyk    2094
1. As mentioned above, edit your post and put the code in [source] tags so that it's readable.

2. All you're telling us is that 'it doesn't work', which quite honestly isn't very helpful, as we then just have to guess at the error. Is the output incorrect? Is the program crashing? Is it a compile-time error? We need more info.

3. In C++, use std::vector rather than new/delete for simple dynamic memory management such as in your example. Among other things, it will give you deep copy semantics 'for free' (bear in mind though that such copies will incur some run-time cost).

4. Returning data_[0] when an index is out of range isn't very logical behavior. You should assert, throw, or log an error instead.

I wasn't able to spot the error in your code, but if you can fix the formatting and provide some info about the problem it'll be a lot easier to do so.

Share this post


Link to post
Share on other sites
Driv3MeFar    1080
Quote:
Original post by lazer_rob
here is the code in C++, a header file, cpp, and a function to test.

i need to do Rst(i,j) = a; in a loop but it doesn't work.
i have tried overloading the = operator and still cannot get it working.

/*RGBclass& operator = (RGBclass c); */
...

RGBvalue a = {255,255,255};
RGBst * Rst = new RGBst(arrayWidth,arrayHeight);
for (int i= 0; i > arrayWidth; i++)
{
for (int j= 0; j > arrayHeight; j++)
{
Rst(i,j) = a;
}
}


RGBst's (commented out) operator= overload expects an RGBclass. You are trying to assign and RGBvalue. This is a problem. Probably not the only problem (see jyk's post), but its probably why your attempt at overloading the operator= did not work.

Either provide RGBst with an operator= that takes an RGBvalue, or utilize RGBclass's constructor that takes an r, g, and b value (and uncomment out/implement the operator= in RGBst):


RGBvalue a = {255,255,255};
RGBst * Rst = new RGBst(arrayWidth,arrayHeight);
for (int i= 0; i > arrayWidth; i++)
{
for (int j= 0; j > arrayHeight; j++)
{
Rst(i,j) = RGBclass(a.r, a.g, a.b);
}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this