accessing a variable from another method

This is a discussion on accessing a variable from another method within the C++ Programming forums, part of the General Programming Boards category; Hey, I have trouble accessing a matrix from another method. I have a matrix allocated like this in one method ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Location
    Barcelona
    Posts
    41

    accessing a variable from another method

    Hey,
    I have trouble accessing a matrix from another method. I have a matrix allocated like this in one method
    Code:
    DTM **dtmpixel = new DTM*[ny_dtm];
    for (int k=0; k<ny_dtm; k++)
    {
    	dtmpixel[k] = new DTM[nx_dtm];
    }
    How can I access it from another method?
    Regards

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    You would pass the pointer to that second function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2008
    Location
    Barcelona
    Posts
    41
    That sounds good =) How would I go about doing that?
    Cheers

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Well, besides passing the pointer (of type DTM**), you would also need to pass the dimensions of the matrix (i.e., ny_dtm and nx_dtm) in order for that function to correctly access the elements of the matrix.

    By the way, are you aware of containers like std::vector that would help you avoid having to manually manage memory?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2008
    Location
    Barcelona
    Posts
    41
    Thanks for your reply, I am aware of vectors, but I wanted to try the "normal way" first. Now it seems to work except for I dont know how to pass nx_dtm and ny_dtm. My function who creates the DTM** now returns it to main and the next method can "take it". But I can only return one value, right?

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    In C++, vectors are the "normal way".

    You can only return 1 value from a function, but you can pass pointer or reference parameters to a function and modify their values, thereby "returning" more than 1 value.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Registered User
    Join Date
    Jul 2008
    Location
    Barcelona
    Posts
    41
    Should I from say function a call function b with the pointer, and additional, dimension data as arguments instead of returning the data as I did now? And how could I create the same matrix in first post with vectors? I tried it but... it didnt work out =/
    Thanks!

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Quote Originally Posted by larne
    Should I from say function a call function b with the pointer, and additional, dimension data as arguments instead of returning the data as I did now?
    That would be one option. An even better option, moving towards creating a full blown matrix class, would be to define:
    Code:
    struct Matrix
    {
        DTM** dtmpixel;
        size_t ny_dtm;
        size_t nx_dtm;
    };
    Then you can pass a Matrix pointer to the function that initialises the Matrix object (and later on this initialisation function would become a constructor when you decide to write a proper class).

    Quote Originally Posted by larne
    And how could I create the same matrix in first post with vectors?
    Code:
    std::vector<std::vector<DTM> > dtmpixel(ny_dtm, std::vector<DTM>(nx_dtm));
    Basically, you create a vector of ny_dtm number of vectors and initialise each of them with nx_dtm number of DTM objects.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jul 2008
    Location
    Barcelona
    Posts
    41
    Perfect, thanks!

  10. #10
    Registered User
    Join Date
    Jul 2008
    Location
    Barcelona
    Posts
    41
    there was no problem just changing the allocation to vector style and using the same code except for passing the vector back to main. Im not sure what´s going wrong here (but I did have some beers doing it). I tried putting receiving datatype as vector<DTM>. Is "vector" enough? No my compilation error is
    Code:
    error: conversion from `std::vector<std::vector<RADAR, std::allocator<RADAR> >, std::allocator<std::vector<RADAR, std::allocator<RADAR> > > >' to non-scalar type `std::vector<RADAR, std::allocator<RADAR> >' requested|
    The vector I am passing on is vector<RADAR> containing instances of DTM. Or if if this is confusing how do I pass a vector to another function?
    Any idea?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    What is your code?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Jul 2008
    Location
    Barcelona
    Posts
    41
    to make it short,
    header:
    Code:
    // headers
    
    // Constants
    //#define M_PI  3.1415926535897932384626433832795
    
    // classes
    class DTM
    {
    ...
    
    };
    
    class RADAR
    {
    
    	vector<DTM> radarpixel;
    
    	public:
    	void adddata(DTM pt)
    	{
    		radarpixel.push_back(pt);
    	}
    	int getsize()
    	{
    		return radarpixel.size();
    	}
    	DTM getdata(int n)
    	{
    		return radarpixel[n];
    	}
    
    };
    
    // funcions
    vector<RADAR> a();
    void b(vector<RADAR>);
    main:
    Code:
    #include "header.h"
    
    int main()
    {
    
    	vector<RADAR> radar = a();
    
    	b(radar);
    
    	return 0;
    }
    function a.
    Code:
    vector<RADAR> a()
    {
    	...
        vector<vector<DTM> > dtmpixel(ny_dtm, vector<DTM>(nx_dtm));
    
        vector<vector<RADAR> > radarpixel(ny_radar, vector<RADAR>(nx_radar));
    	
        radarpixel[tmplin][tmpcol+midcol].adddata(dtmpixel[y][x]);
    
        return radarpixel;
    }
    function b:
    Code:
    #include "header.h"
    
    void b(vector<RADAR> radarpixel)
    {
    ...
    }

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Well, you declared radarpixel to be a vector<vector<RADAR> >, but then you return it from a(), whose return type is vector<RADAR>.

    By the way, unless you really want a copy, it would usually be better to take the arguments by (const) reference.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Jul 2008
    Location
    Barcelona
    Posts
    41
    ok, being a rookie and all, how could I return a copy correctly? ...and I dont really understand (const) reference. Pass the pointer? In this case, how?
    Thanks for your help

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Quote Originally Posted by larne
    ok, being a rookie and all, how could I return a copy correctly?
    You're returning a copy correctly, but copying could be expensive if certain compiler applied optimisations are not applicable.

    Quote Originally Posted by larne
    and I dont really understand (const) reference. Pass the pointer? In this case, how?
    I mean:
    Code:
    void b(vector<RADAR>& radarpixel)
    {
        // ...
    }
    or:
    Code:
    void b(const vector<RADAR>& radarpixel)
    {
        // ...
    }
    If you don't understand the above syntax then you need to read a good introductory book on C++.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing variables with memory address
    By ITAmember in forum C Programming
    Replies: 54
    Last Post: 06-28-2009, 03:35 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Problem with a char variable set as a letter
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2004, 12:25 PM
  5. Replies: 3
    Last Post: 12-03-2001, 12:45 PM

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