Simple vector class problem, please help

This is a discussion on Simple vector class problem, please help within the C++ Programming forums, part of the General Programming Boards category; Can I have something like nn[i] as a array to store the data, such that in a loop of all ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    12

    Simple vector class problem, please help

    Can I have something like nn[i] as a array to store the data, such that in a loop of all 8nn value I can have site[index].getnn[i]() from i=0-7 rather than using nn1 to nn8 again and again?

    Thank you very much!!

    Code:
    #include <iostream> // Include input/output stream
    
    #include <fstream>  // Include file output stream
    
    #include <cmath>   // Include math function, sin, cos
    
    
    
    class sitelist
    
    
    
    {
    
    private:
    
    	int state, nn1, nn2, nn3, nn4, nn5, nn6, nn7, nn8, xcoord, ycoord, zcoord;
    
    
    public:
    
    // Default constructor
    
    	sitelist() 
    
    	{
    
    		state= 0;
    		nn1=0;
    		nn2=0;
    		nn3=0;
    		nn4=0;
    		nn5=0;
    		nn6=0;
    		nn7=0;
    		nn8=0;
    
    		xcoord = 0;
    
    		ycoord = 0;
    
    		zcoord = 0;
    
    	}
    
    
    
    // Assess method for xyzcoord
    
    	int getstate()
    
    	{
    
    		return state;
    
    	}
    	int getnn1()
    	{
    		return nn1;
    	}
    	int getnn2()
    	{
    		return nn2;
    	}
    	int getnn3()
    	{
    		return nn3;
    	} 
    	int getnn4()
    	{
    		return nn4;
    	} 
    	int getnn5()
    	{
    		return nn5;
    	} 
    	int getnn6()
    	{
    		return nn6;
    	} 
    	int getnn7()
    	{
    		return nn7;
    	} 
    	int getnn8()
    	{
    		return nn8;
    	}
    
    	int getx()
    
    	{
    
    		return xcoord;
    
    	}
    
    	int gety()
    
    	{
    
    		return ycoord;
    
    	}
    
    	int getz()
    
    	{
    
    		return zcoord;
    
    	}
    
    
    	sitelist(int st, int nn_1, int nn_2, int nn_3, int nn_4, int nn_5, int nn_6, int nn_7, int nn_8, int x, int y, int z)
    
    	{
    
    		state=st;
    		nn1=nn_1;
    		nn2=nn_2;
    
    		nn3=nn_3;
    		nn4=nn_4;
    		nn5=nn_5;
    		nn6=nn_6;
    		nn7=nn_7;
    		nn8=nn_8;
    		xcoord = x;
    
    		ycoord = y;
    
    		zcoord = z;
    
    	}
    
    
    
    	void setstate(int value)
    
    	{
    
    		state=value;
    
    	}
    	void setnn1(int value)
    
    	{
    
    		nn1=value;
    
    	}
    	void setnn2(int value)
    
    	{
    
    		nn2=value;
    
    	}
    	void setnn3(int value)
    
    	{
    
    		nn3=value;
    
    	}
    	void setnn4(int value)
    
    	{
    
    		nn4=value;
    
    	}	
    	void setnn5(int value)
    
    	{
    
    		nn5=value;
    
    	}	
    	void setnn6(int value)
    
    	{
    
    		nn6=value;
    
    	}	
    	void setnn7(int value)
    
    	{
    
    		nn7=value;
    
    	}
    	void setnn8(int value)
    
    	{
    
    		nn8=value;
    
    	}
    
    	void setx(int value)
    
    	{
    
    		xcoord=value;
    
    	}
    
    	void sety(int value)
    
    	{
    
    		ycoord=value;
    
    	}
    
    	void setz(int value)
    
    	{
    
    		zcoord=value;
    
    	}
    
    
    
    // Method to print out contents to screen
    
    	void print()
    
    	{
    
    		std::cout  << xcoord << '\t'
    
    				        << ycoord << '\t'
    
    				        << zcoord << '\t'
    				        << state << '\t' 
    				    	<<nn1<<'\t'
    				    	<<nn2<<'\t'
    
    				    	<<nn3<<'\t'
    				    	<<nn4<<'\t'
    				    	<<nn5<<'\t'
    				    	<<nn6<<'\t'
    				    	<<nn7<<'\t'
    				    	<<nn8<<std::endl;
    
    	}
    
    
    
    // Method to print out contents on a file
    
    	void print(std::ofstream& fout)
    
    	{
    
    		fout << xcoord << '\t'
    
    			   << ycoord << '\t'
    
    			   << zcoord <<'\t'
    		      << state << '\t'
    				<<nn1<<'\t'
    				<<nn2<<'\t'
    
    				<<nn3<<'\t'
    				<<nn4<<'\t'
    				<<nn5<<'\t'
    				<<nn6<<'\t'
    				<<nn7<<'\t'
    				<<nn8<<std::endl;
    
    	}
    
    
    
    };

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    umm basicly what your saying will work, except you will have to pass an argument to the get function to return the proper value, a simple switch statement would work. That and a little advice would be to place the class in a header, and all of the function code in a source file. That way everything is nice and neat for you later on. look through classes that are all coded in one file is a pain in the ass.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, you can and should use an array for nn1..nn8.

    Code:
    int nn[8];

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    12
    Quote Originally Posted by anon View Post
    Yes, you can and should use an array for nn1..nn8.

    Code:
    int nn[8];

    Thank you for replying. But when I am trying to get the value, I use these. And it return as invalid member function declaration, how can I solve this? Thanks.

    Code:
    	int getnn[]()
    	{
    		return nn[];
    	}
    
    	void setnn[8](int value)
    
    	{
    
    		nn[8]=value;
    
    	}
    Last edited by fidodidohk; 03-30-2007 at 04:44 AM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Like so
    Code:
    	int getnn(int which)
    	{
    		return nn[which];
    	}
    
    	void setnn(int which, int value)
    	{
    		nn[which]=value;
    	}
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    12
    Quote Originally Posted by Salem View Post
    Like so
    Code:
    	int getnn(int which)
    	{
    		return nn[which];
    	}
    
    	void setnn(int which, int value)
    	{
    		nn[which]=value;
    	}
    Thank you very much. But after I have try, I have got a several error. I am sorry, I am a bit dump in C++, would you mind help me by teaching me slightly more explicitly? Thank you very much!!


    Code:
    #include <iostream> // Include input/output stream
    #include <fstream>  // Include file output stream
    #include <cmath>   // Include math function, sin, cos
    
    class sitelist
    {
    private:
    	int state, nn[8], xcoord, ycoord, zcoord;
    public:
    
    // Default constructor
    	sitelist() 
    	{
    		state= 0;
    		nn[0]=0;
    		nn[1]=0;
    		nn[2]=0;
    		nn[3]=0;
    		nn[4]=0;
    		nn[5]=0;
    		nn[6]=0;
    		nn[7]=0;
    		xcoord = 0;
    		ycoord = 0;
    		zcoord = 0;
    	}
    	int getstate()
    	{
    		return state;
    	}
    	int getnn(int 0)
    	{
    		return nn[0];
    	}
    	int getnn(int 1)
    	{
    		return nn[1];
    	}
    	int getnn(int 2)
    	{
    		return nn[2];
    	}
    	int getnn(int 3)
    	{
    		return nn[3];
    	}	
    	int getnn(int 4)
    	{
    		return nn[4];
    	}	
    	int getnn(int 5)
    	{
    		return nn[5];
    	}
    	int getnn(int 6)
    	{
    		return nn[6];
    	}	
    	int getnn(int 7)
    	{
    		return nn[7];
    	}
    	int getx()
    	{
    		return xcoord;
    	}
    	int gety()
    	{
    		return ycoord;
    	}
    	int getz()
    	{
    		return zcoord;
    	}
    	sitelist(int st, int nn_[8], int x, int y, int z)
    	{
    		state=st;
    		nn[8]=nn_[8];
    		xcoord = x;
    		ycoord = y;
    		zcoord = z;
    	}
    	void setstate(int value)
    	{
    		state=value;
    	}
    	void setnn(int 0, int value)
    	{
    		nn[0]=value;
    	}
    
    	void setnn(int 1, int value)
    	{
    		nn[1]=value;
    	}
    	void setnn(int 2, int value)
    	{
    		nn[2]=value;
    	}
    	void setnn(int 3, int value)
    	{
    		nn[3]=value;
    	}
    	void setnn(int 4, int value)
    	{
    		nn[4]=value;
    	}
    	void setnn(int 5, int value)
    	{
    		nn[5]=value;
    	}
    	void setnn(int 6, int value)
    	{
    		nn[6]=value;
    	}
    	void setnn(int 7, int value)
    	{
    		nn[7]=value;
    	}	void setx(int value)
    	{
    		xcoord=value;
    	}
    	void sety(int value)
    	{
    		ycoord=value;
    	}
    	void setz(int value)
    	{
    		zcoord=value;
    	}
    };
    Error message
    Code:
    sitelist.h:34: error: expected ‘,’ or ‘...’ before numeric constant
    sitelist.h:38: error: expected ‘,’ or ‘...’ before numeric constant
    sitelist.h:38: error: ‘int sitelist::getnn(int)’ cannot be overloaded
    sitelist.h:34: error: with ‘int sitelist::getnn(int)’
    sitelist.h:42: error: expected ‘,’ or ‘...’ before numeric constant
    sitelist.h:42: error: ‘int sitelist::getnn(int)’ cannot be overloaded
    sitelist.h:34: error: with ‘int sitelist::getnn(int)’
    sitelist.h:46: error: expected ‘,’ or ‘...’ before numeric constant
    sitelist.h:46: error: ‘int sitelist::getnn(int)’ cannot be overloaded
    sitelist.h:34: error: with ‘int sitelist::getnn(int)’
    sitelist.h:50: error: expected ‘,’ or ‘...’ before numeric constant
    sitelist.h:50: error: ‘int sitelist::getnn(int)’ cannot be overloaded
    sitelist.h:34: error: with ‘int sitelist::getnn(int)’
    sitelist.h:54: error: expected ‘,’ or ‘...’ before numeric constant
    sitelist.h:54: error: ‘int sitelist::getnn(int)’ cannot be overloaded
    sitelist.h:34: error: with ‘int sitelist::getnn(int)’
    sitelist.h:58: error: expected ‘,’ or ‘...’ before numeric constant
    sitelist.h:58: error: ‘int sitelist::getnn(int)’ cannot be overloaded
    sitelist.h:34: error: with ‘int sitelist::getnn(int)’
    sitelist.h:62: error: expected ‘,’ or ‘...’ before numeric constant
    sitelist.h:62: error: ‘int sitelist::getnn(int)’ cannot be overloaded
    sitelist.h:34: error: with ‘int sitelist::getnn(int)’
    sitelist.h:92: error: expected ‘,’ or ‘...’ before numeric constant
    sitelist.h:96: error: expected ‘,’ or ‘...’ before numeric constant
    sitelist.h:96: error: ‘void sitelist::setnn(int)’ cannot be overloaded
    sitelist.h:92: error: with ‘void sitelist::setnn(int)’
    sitelist.h:100: error: expected ‘,’ or ‘...’ before numeric constant
    sitelist.h:100: error: ‘void sitelist::setnn(int)’ cannot be overloaded
    sitelist.h:92: error: with ‘void sitelist::setnn(int)’
    sitelist.h:104: error: expected ‘,’ or ‘...’ before numeric constant
    sitelist.h:104: error: ‘void sitelist::setnn(int)’ cannot be overloaded
    sitelist.h:92: error: with ‘void sitelist::setnn(int)’
    sitelist.h:108: error: expected ‘,’ or ‘...’ before numeric constant
    sitelist.h:108: error: ‘void sitelist::setnn(int)’ cannot be overloaded
    sitelist.h:92: error: with ‘void sitelist::setnn(int)’
    sitelist.h:112: error: expected ‘,’ or ‘...’ before numeric constant
    sitelist.h:112: error: ‘void sitelist::setnn(int)’ cannot be overloaded
    sitelist.h:92: error: with ‘void sitelist::setnn(int)’
    sitelist.h:116: error: expected ‘,’ or ‘...’ before numeric constant
    sitelist.h:116: error: ‘void sitelist::setnn(int)’ cannot be overloaded
    sitelist.h:92: error: with ‘void sitelist::setnn(int)’
    sitelist.h:120: error: expected ‘,’ or ‘...’ before numeric constant
    sitelist.h:120: error: ‘void sitelist::setnn(int)’ cannot be overloaded
    sitelist.h:92: error: with ‘void sitelist::setnn(int)’
    sitelist.h: In member function ‘void sitelist::setnn(int)’:
    sitelist.h:94: error: ‘value’ was not declared in this scope
    sitelist.h: In member function ‘void sitelist::setnn(int)’:
    sitelist.h:98: error: ‘value’ was not declared in this scope
    sitelist.h: In member function ‘void sitelist::setnn(int)’:
    sitelist.h:102: error: ‘value’ was not declared in this scope
    sitelist.h: In member function ‘void sitelist::setnn(int)’:
    sitelist.h:106: error: ‘value’ was not declared in this scope
    sitelist.h: In member function ‘void sitelist::setnn(int)’:
    sitelist.h:110: error: ‘value’ was not declared in this scope
    sitelist.h: In member function ‘void sitelist::setnn(int)’:
    sitelist.h:114: error: ‘value’ was not declared in this scope
    sitelist.h: In member function ‘void sitelist::setnn(int)’:
    sitelist.h:118: error: ‘value’ was not declared in this scope
    sitelist.h: In member function ‘void sitelist::setnn(int)’:
    sitelist.h:122: error: ‘value’ was not declared in this scope
    Last edited by fidodidohk; 03-30-2007 at 06:05 AM.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    You only need ONE setnn function (with 2 parameters)

    Or you could do it like
    Code:
    	void set0(int value)
    	{
    		nn[0]=value;
    	}
    
    	void set1(int value)
    	{
    		nn[1]=value;
    	}
    The bottom line is that numbers are not variable names, which is where all the error messages are coming from.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    12
    Quote Originally Posted by Salem View Post
    You only need ONE setnn function (with 2 parameters)

    Or you could do it like
    Code:
    	void set0(int value)
    	{
    		nn[0]=value;
    	}
    
    	void set1(int value)
    	{
    		nn[1]=value;
    	}
    The bottom line is that numbers are not variable names, which is where all the error messages are coming from.


    Thank you. But the reason I would like to have get[] and set[] is because I would like to loop it in the main, however with set1 as a function I will not be able to loop it.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Thank you. But the reason I would like to have get[] and set[] is because I would like to loop it in the main, however with set1 as a function I will not be able to loop it.
    You don't need square brackets, because you can call a normal function in a loop too.

    Code:
    void setNN(int which, int value)
    {
        nn[which] = value;
    }
    
    for (int i = 0; i < nnSize; ++i)
    {
        setNN(i, some_value);
    }
    Alternatively you can overload the [] operator.

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    12
    O!! It works perfectly now!!
    Thanks a lot guys!! Really appreciate it!!
    I hope I am good enough to help people one day!!
    Many thanks!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 01:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 08:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21