Arrays: defining a new class in c++

This is a discussion on Arrays: defining a new class in c++ within the C++ Programming forums, part of the General Programming Boards category; Dear All, I am trying to make class which uses an array to give a periodic function and am having ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    4

    Arrays: defining a new class in c++

    Dear All,

    I am trying to make class which uses an array to give a periodic function and am having some trouble!

    Effectively, I am trying to make an array such that, for any point (i,j) in a 2D matrix, of size LxL, the command "next_i[i]" will return "(i+1)%L", enabling the selection of a point to the right of any point in the matrix (next[i], j)

    I have tried adapting some code given to us in lectures ("Matrix(i,j)").

    Here's my code:

    Code:
    #include <vector>
    
    class Matrix
    {
    private:
      std::vector<double>	m_data;
      int			m_rows;
      int			m_cols;
    
    
    public:
      Matrix( int n_rows, int n_cols, int init = 1 )
        : m_data( n_rows * n_cols, init ),
          m_rows( n_rows ), m_cols( n_cols ) {}
    
      double& operator()(int i, int j)
      {
         return m_data[i*m_cols + j];
      }
    
    /*Start of my coding*/
    public:
       next_i( int n_cols )
        : m_data(n_cols),
          m_cols(n_cols) {}
    
      double& operator()(int i)
      {
         m_data[i] = (i + 1)% m_cols;
         return m_data[i];
      }
    /*End of my coding*/
    
    };
    And here's what the compiler is telling me:

    pccaths25:Testing$ g++ -g -pedantic -Wall $PT2_INC -o test.exe test.cc $PT2_LIB
    In file included from test.cc:1:
    Matrix2.h:23: error: ISO C++ forbids declaration of ‘next_i’ with no type
    Matrix2.h: In member function ‘int Matrix::next_i(int)’:
    Matrix2.h:24: error: only constructors take base initializers
    Matrix2.h:25: warning: no return statement in function returning non-void
    pccaths25:Testing$

    I am new to c++ and am not quite sure what this is telling me - suggestions would be greatly appreciated. Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It means what it says: you are trying to declare a function without a return type, which isn't allowed. You are trying to use a constructor list (the : m_data part) in something that isn't a constructor. I'm not quite certain whether you're trying to make a new class or add to your existing class.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    next_i should have a return type, and it cannot use an initializer list. Since it is not a constructor, it makes no sense to give it one. Assign those variables inside the function body.

    If next_i returns nothing, make it of type void.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    Quote Originally Posted by tabstop View Post
    I'm not quite certain whether you're trying to make a new class or add to your existing class.
    I guess I am trying to make a new class - all I need is a function that will return the next largest number in a periodic fashion, from a set of natural numbers, L. ( i.e. next_i[0] = 1 ... next_i[L-1] = 0 ).

    I don't really have clue how to do this, and was just using the code I was given (above mine) and trying something - I'd really appreciate if you could please suggest some code for me to try. Many thanks.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by jeremy-hulse View Post
    I guess I am trying to make a new class - all I need is a function that will return the next largest number in a periodic fashion, from a set of natural numbers, L. ( i.e. next_i[0] = 1 ... next_i[L-1] = 0 ).
    This sentence is inconsistent with itself. If "all you need" is a function, then you most certainly aren't "trying to make a new class".

    Are you wedded to this array notation thing? If you're willing to use parentheses instead of brackets, then all you need is really a function (no classes involved).

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    I appreciate what you're saying - writing in the programme instead, a function like:

    Code:
    int next_i(int i) {
      int next = (i+1)%L;
      return next;
    }
    Please correct me if i am wrong, but I thought that perhaps it would be quicker computationally to use an array with predefined values, rather than use a function to calculate the next number every time. (I would be using this function several times, for the same value of L). Thoughts welcome. Thanks

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The process of computing this thing every time is an addition, and a division (plus the overhead of a function call, which in this case is pretty small). The process of accessing an array element is an addition and a multiplication (to find the proper memory address) (plus the overhead of fetching the memory itself). So the run-time performance would be pretty even. The array has the added disadvantage of having to do the precomputation.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    Okay, fair enough. I wasn't aware of that. Thanks very much for your help - much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Base Class Arrays
    By Lurker in forum C++ Programming
    Replies: 14
    Last Post: 03-12-2004, 12:43 PM
  2. defining and using a global class
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2004, 09:51 PM
  3. Replies: 1
    Last Post: 04-20-2003, 06:02 PM
  4. arrays as class names
    By Katle in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2002, 03:58 PM
  5. Do you store store one off data arrays in a class?
    By blood.angel in forum C++ Programming
    Replies: 5
    Last Post: 06-24-2002, 01:05 PM

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