void* error

This is a discussion on void* error within the C++ Programming forums, part of the General Programming Boards category; Hi all. I've a problem with the void*. I explain it: Code: void leggi_vettore(void* & v, int n, int t){ ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    void* error

    Hi all.

    I've a problem with the void*. I explain it:

    Code:
    void leggi_vettore(void* & v, int n, int t){
    	if(t==1){
    		v = new float[n];
    	}else if(t==0){
    		v = new unsigned short[n];
    	}else cout << "Error!";
    	for (int i=0 ; i<n ; i++){
    		cout << "Inserimento in posizione [ " << i << " ]: ";
    		cin >> v[i];
    	}
    }
    I want to have a dynamic array and I want to decide with "int t" if it must be float or unsigned short.
    When I did "cin >> v[i];" the compiler makes this error: "void* is not a pointer to object type".

    What I can do?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,669
    You need to convert v to an appropriate (non-void) pointer type, otherwise v[i] is invalid (there is no such thing in C++ as an array of voids).

    All the compiler knows about v in your last loop is that it is a pointer to void. It does not magically keep track of the fact you've created arrays of different types just beforehand - hence the complaint.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

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, 09:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 10:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

Tags for this Thread


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