Cannot access pointers from an array

This is a discussion on Cannot access pointers from an array within the C++ Programming forums, part of the General Programming Boards category; Hi all I'm a fair beginner with C++, but try bear with me. I'm having problems accessing pointers that I've ...

  1. #1
    The 7th Coder Datamike's Avatar
    Join Date
    Jan 2002
    Posts
    12

    Cannot access pointers from an array

    Hi all

    I'm a fair beginner with C++, but try bear with me. I'm having problems accessing pointers that I've stored into an array. The idea in my program is to have an array, and as the program is executed create a set of objects (which store various data). Later than in the program, I can't access that data by looping through the array that contains the pointers to these objects.

    The issue rises when I try to access the data. I'll try post the relevant part of my code here:

    Code:
    // my class
    class objVaraus
    {
    public:
      USHORT vrPaiva;
      USHORT vrPituus;
      string vrVaraaja;
    
      void tarkistus();
      int teevaraus();
    };
    
    ====
    
    // this is in the main() program
    
    objVaraus *pList[31]
    objVaraus *pVaraus = new objVaraus;
    objVaraus varaus;
    cout << "\n" << *pVaraus << "\n";
    The idea is that the pointer in *pVaraus is stored into *pList[31].

    The array that stores the pointers isn't there, because I get a very long list of completely incomprehensible output when I compile it. I get the same thing with this one. I can post a part of it later, if it's necessary, though I doubt it.

    Hope I'm making sense. Essentially, am I doing something fundamentally wrong with this? And how do I actually access the pointers that are in the array?

    I'd appreciate all tips and ideas. Thanks.
    developer & programmer
    me tomi kaistila
    gnupg 0x6D58CC04
    home http://www.datamike.org


  2. #2
    CS Major Glorfindel-RW's Avatar
    Join Date
    Nov 2005
    Posts
    5
    I believe you would be looking for something along these lines...

    Code:
    	objVaraus *pList[31];
    	objVaraus *pVaraus = new objVaraus;
    	
    	pList[0] = pVaraus;
    
    	cout << "\n" << pVaraus << "\n";
    I'm assuming in this, that you have already overloaded the stream (<<) operator for the objVaraus class. Otherwise you would need to use ... pVaraus->vrVaraaja to display the string, etc etc...

    pList[0] = new objVaraus; would also work.

    I think this is the answer to what you were asking... if not, please clarify and I will try again.

  3. #3
    The 7th Coder Datamike's Avatar
    Join Date
    Jan 2002
    Posts
    12
    Thanks for the reply. Basically what you posted, is what I have. But, it only works to display the memory address stored in pVaraus, when what I want to do, is display some of the content stored in the memory of that address. I tried this:

    Code:
    objVaraus *pList[31];
    objVaraus *pVaraus = new objVaraus;
    	
    pList[0] = pVaraus;
    
    cout << "\n" << *pList[0] << "\n";
    But only got something like this:

    pirtin_varaus.cpp: In function `int main()':
    pirtin_varaus.cpp:65: error: no match for 'operator<<' in 'std:perator<<
    [with _Traits = std::char_traits<char>]((&std::cout), "\n") << *pList[0]'
    /usr/include/c++/3.3.4/bits/ostream.tcc:63: error: candidates are:
    std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
    _Traits>:perator<<(std::basic_ostream<_CharT,
    _Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char,
    _Traits = std::char_traits<char>]
    Then I tried this:

    Code:
    objVaraus *pVaraus = new objVaraus;
    objVaraus varaus;
    pList[0] = pVaraus;
    
    cout << "Anna varaajan nimi: ";
    cin >> varaus.vrVaraaja;
    cout << "\n" << *pList[0]->vrVaraaja << "\n";
    And I get this:

    pirtin_varaus.cpp: In function `int main()':
    pirtin_varaus.cpp:65: error: no match for 'operator*' in '*
    pList[0]->objVaraus::vrVaraaja'
    So, I can display the memory address, but I can't display the content of that memory slot...
    developer & programmer
    me tomi kaistila
    gnupg 0x6D58CC04
    home http://www.datamike.org


  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Code:
    cout << "\n" << *pList[0] << "\n";
    would work but you need to define an overloaded operator<< that can handle a objVaraus

    Code:
    cout << "\n" << *pList[0]->vrVaraaja << "\n";
    also will work but you need to remove the leading * or change the -> to a .

    Rember pList[0] returns a pointer, putting * in front returns the object it points to. -> accesses a member given a pointer, . accesses a member given the object.
    Last edited by Darryl; 11-04-2005 at 10:11 AM.

  5. #5
    The 7th Coder Datamike's Avatar
    Join Date
    Jan 2002
    Posts
    12
    Thanks a million Glorfindel-RW and Darryl. I managed to solve the problem. Also learned tons. I really appreciate it
    developer & programmer
    me tomi kaistila
    gnupg 0x6D58CC04
    home http://www.datamike.org


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  2. Replies: 4
    Last Post: 11-02-2006, 10:41 AM
  3. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM
  4. Loading an array using pointers
    By cheesehead in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2001, 04:23 AM
  5. Creating an array of object pointers
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 10:01 PM

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