Thread: How to access functions from another class in C++?

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    3

    How to access functions from another class in C++?

    I am trying to create a class called array whic is based on class vector. I use the template class for generalized data type and some constructors to reserve the memory for the array.
    I ran into those error without any idea how to fix them. Please help me to explain the error and instruct me to fix them. Thanks

    main.cpp: In function ‘int main(int, char**)’:
    main.cpp:11:22: error: ‘class array<int>’ has no member named ‘size’
    main.cpp:12:6: error: no match for ‘operator[]’ in ‘a[m]’
    main.cpp:13:22: error: no match for ‘operator[]’ in ‘a[m]’
    Compilation failed.
    ----------------------------------------------------------------------
    Code:
    #ifndef ARRAY_H
    #define ARRAY_H
    
    #include <string>           //for std::string
    #include <vector>           //for std::vector
    
    template <typename T> 
    class array 
    {
    private: 
        size_t N1,N2,N3;
        std::vector<T> array1D; 
        std::vector<std::vector<T> > array2D; 
        std::vector<std::vector<std::vector<T> > > array3D;         
    public:    
           // default constructor
        array();
        array(size_t); 
        array(size_t,size_t);
        array(size_t,size_t,size_t);
            
    };
    #endif
    --------------------------------------------------------------
    
    # include "array.h"
    
    template <typename T> 
    array<T>::array(size_t i)
    {
        N1 = i;
        // make room for some elements
        array1D.reserve(N1);
    }
    
    template <typename T> 
    array<T>::array(size_t i,size_t j)
    {
        N1=i;
        N2=j;
        array2D.reserve(N1);
        for (int m=0; m < array2D.size(); m++)
            array2D[m].reserve(N2);        
    }
    
    -------------------------------------------------------
    #include <iostream>            //for std::cout
    #include <vector>           //for std::vector
    #include "array.h"
    
    int main(int argc, char * argv[])
    {
        size_t i=10;
        array<int> a(i);
        
        for (int m=0; m < a.size(); m++) {
            a[m]=m;
            std::cout << a[m] << '\n';
        }
        return 0;
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Aren't these errors self explanatory? There is no size member in array, nor an operator []. Why do you think there is?
    Furthermore, I assume this is just for exercise? Because otherwise there is already std::array.
    Also, don't remove parameter names from the class definitions, because it makes it harder to read and understand your code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2015
    Posts
    3
    Hi Elysia,
    Yes, this is just my practice since I am new to OOP. I'm trying to figure out how to use the member function size and operator [] from vector class for this array class (just like the tittle of this thread). Is there any other way to achieve that?
    Thanks.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You would have to write the size() function yourself if you wanted to use it in your array class. Basically just make sure that your member function always returns the right value for that.

    Using [] is possible for user-defined classes like your array class - it involves something called "operator overloading" which is a very searchable term. There are many sources that can help you implement it properly, if you want to tackle that now. Try reading Overloading the Subscript Operator the Right Way. A lot of operator overloading is typing out the right function, but there are some rules of thumb to consider.

  5. #5
    Registered User
    Join Date
    Jan 2015
    Posts
    3
    @whiteflags, Thanks!!! Your answer is very helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-18-2012, 11:17 AM
  2. Can Nested class access parent class's private member?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 08:42 AM
  3. Replies: 10
    Last Post: 07-26-2008, 08:44 AM
  4. Replies: 25
    Last Post: 10-29-2007, 04:08 PM
  5. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 06:32 PM

Tags for this Thread