What is with this error?

This is a discussion on What is with this error? within the C++ Programming forums, part of the General Programming Boards category; This error is generated 3 times in reference to the code below. It has to do with the [][] operators. ...

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

    What is with this error?

    This error is generated 3 times in reference to the code below. It has to do with the [][] operators. CUDA:: DeviceMemory<> is a class I defined, almost identical to a std::vector<> class type, with the [] operators behaving the same as vector's would. I am lead to think it is being generated by the [] operators, yet I don't know how to solve it, especially since I have it working in another part of the code. Any help on what this error means, or what causes it would be appreciated.
    Code:
    error: operand of "*" must be a pointer
    Code:
    // code that does not compile, and generates above error
    __global__ void Kernel_square2d(CUDA::DeviceMemory< CUDA::DeviceMemory<float> > a)
    {
    	const int idx = blockIdx.x * blockDim.x + threadIdx.x; // these are just for indexing
    	const int idy = blockIdx.y * blockDim.y + threadIdx.y; // indexing
    
    	if (idx < a.size() && idy < a[idx].size()) {
    		a[idx][idy] = a[idx][idy] + a[idx][idy]; // get an error here, 3 times, one for each a[idx][idy]
    	}
    }
    
    // code that does work, HostMemory is virtually identical to DeviceMemory.
            // make a 2D array
    	printf("Allocating a 10x10 2 dimensional array of type float\n");
    
    	HostMemory< HostMemory<float> > twoDimArray;
    	twoDimArray.allocate(10);
    
    	for(unsigned int i = 0; i < twoDimArray.size(); i++) {
    		twoDimArray[i].allocate(10);
    	}
    
    	// assign values to the 2D array
    	printf("Assigning values of 10x10 matrix\n");
    
    	for(unsigned int i = 0; i < twoDimArray.size(); i++) {
    		for(unsigned int n = 0; n < twoDimArray[i].size(); n++) {
    			twoDimArray[i][n] = twoDimArray.size()*i + n;
    		}
    	}

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I imagine the problem is in the class you defined, where you define the "[]" operator.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    I have it defined twice, one for modifying the value, and one for accessing the value. I've used this operator before when I made different 3d vector classes. It still blows my mind as to why it will work in one instance, but not in another.

    memoryType is from the definition of the class, CUDA:: DeviceMemory<class memoryType>. the variable memory that is being returned is a pointer of type memoryType.
    Code:
    // in the definition
    
    private:
    	memoryType* memory;
    
    // elsewhere in the definition
    
    memoryType& operator [] (const int a)
    {
    	return memory[a];
    }
    
    memoryType operator [] (const int a) const
    {
    	return memory[a];
    }

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    You could be using two operator[]s from different classes. Say memoryType is an array class, and the array is a pointer array--the rows of your matrix. memoryType uses it's operator[] to return the pointer element. Then the pointer's operator[] indexes the column part of your matrix.

    There are other things people like to do... like memory(a , b). Or you could use member functions instead of overloading operators.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by whiteflags View Post
    You could be using two operator[]s from different classes. Say memoryType is an array class, and the array is a pointer array--the rows of your matrix. memoryType uses it's operator[] to return the pointer element. Then the pointer's operator[] indexes the column part of your matrix.

    There are other things people like to do... like memory(a , b). Or you could use member functions instead of overloading operators.
    I did not think of that, and that would make total sense since that is basically what is happening, and that's a very good solution. I may go that route and "fake" a 2 dimensional array. I am trying to program this in to a CUDA kernel for an nVidia card (if anyone has heard of this). Turns out 2 dimensional arrays are not advised on kernels since allocating a 2D array on the GPU is still in a linear block or memoy anywho (possibly on the host also?). The best bet is to flatten the 2 dimensional array in to a single dimension.

    Or maybe I'll just stick with a single dimensioned array since that works perfectly

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,244
    Your class might also support a unary operator *(), and that is causing some form of ambiguity.

    More generally, however, you will have more chance finding the cause of your problem by providing a small but complete example of code that exhibits your concern. Paraphrasing, as you are, means you are making an assumption about where the problem is. When people do that, they often leave out a key bit of information that is relevant to solving their problem because they considered it irrelevant.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,535
    Quote Originally Posted by scwizzo View Post
    I did not think of that, and that would make total sense since that is basically what is happening, and that's a very good solution. I may go that route and "fake" a 2 dimensional array. I am trying to program this in to a CUDA kernel for an nVidia card (if anyone has heard of this). Turns out 2 dimensional arrays are not advised on kernels since allocating a 2D array on the GPU is still in a linear block or memoy anywho (possibly on the host also?). The best bet is to flatten the 2 dimensional array in to a single dimension.

    Or maybe I'll just stick with a single dimensioned array since that works perfectly
    You can make a 2D array contiguous.
    Example using new:
    int (*p)[100] = new int[100][100];
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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