"variable or field declared void"

This is a discussion on "variable or field declared void" within the C++ Programming forums, part of the General Programming Boards category; Can somebody explain the point of the compiler's message:"variable or field `a' declared void "? I'm not alone. If you ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Location
    Budapest
    Posts
    26

    Question "variable or field declared void"

    Can somebody explain the point of the compiler's message:"variable or field `a' declared void "?
    I'm not alone. If you put this message without `a' into the google's browser, you will get 10 100 000 results.
    I'm beyond the first steps in c++. At least, I thought so far. I must admit that I don't understand the datatype void at all.
    Here's a simply, short code which doesn't work due to use of datatype void:

    #include<iostream>
    using namespace std;

    int main(){

    void a, b;

    cout<<"a = ";cin>>a; cout<<"\n\tb = ";cin>>b;

    cin.get();

    }

    To be exact, yet the compilation was failed.
    Last edited by Treborh; 02-05-2010 at 10:19 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If I'm not mistaken, you simply can't declare variables of "type" void. That is, void is not a type, it means lack of type (e.g where a function doesn't return anything).

    If you've seen it in a function "argument" declaration:

    Code:
    int foo(void);
    then again, it doesn't mean the function takes one argument of void type. It means the function doesn't take any arguments, and in C++ it has exactly the same meaning as

    Code:
    int foo();
    Last edited by anon; 02-05-2010 at 10:32 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Feb 2010
    Location
    Budapest
    Posts
    26
    Thanks. But I have already have some expierence in another a bit higher level language, namely Visual Basic 6.0 for application (Excel-but it doesn't matter). So it is sure that this confuses me. You know, in that language void is a datatype which cover somehow almost the same concept.
    But there it works like a jolyjoker you can use this key-word as an implicit typecaster. When you edit your code and don't know what type of data you will put in the variable you use it simply.

    So you say that this key-word has no such role in c-language that I detailed above. If I make out also this sense of your message, I don't make a mistake, do I?
    Last edited by Treborh; 02-05-2010 at 11:07 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Yes, void cannot be used in that way. However, when it comes to pointers, a pointer to void can be used in that way when dealing with pointers to 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

  5. #5
    Registered User
    Join Date
    Feb 2010
    Location
    Budapest
    Posts
    26
    Thanks a lot. It was a more concrete and I will visit the recommended links.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,348
    you can think of variables in C++ as balls on a pool table. solid-colored balls are pointers, and stripes are objects. every solid has a corresponding stripe of the same color, except the 8-ball, just like you can instantiate an object of any type you can create a pointer to, except a void pointer. there is no void object type.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    That's a terrble analogy Elkvis.

    You can't declare a void variable because it occupies no space. It would be like building a bridge out of a vacuum.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,536
    I would not suggest using void* either, however. If you need a function that takes a generic type, you should look into templates.
    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.

  9. #9
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by Elysia View Post
    I would not suggest using void* either, however. If you need a function that takes a generic type, you should look into templates.
    Unless size is a consideration or your platform doesn't support them...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  10. #10
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    Quote Originally Posted by jeffcobb View Post
    Unless size is a consideration or your platform doesn't support them...
    Or your building an API (no templates for APIs). Threading is another place where void*'s are pretty much unavoidable.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    Void is a type. You can instantiate templates with it. You just can't have variables of (cv-qualified) void or void &.

    Void pointer is special because any non-function pointer type is implicitly convertible to (and in C form) void * (as long cv correctness is preserved). This makes it a good generic pointer.
    Last edited by King Mir; 02-06-2010 at 01:43 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Quote Originally Posted by Cogman
    Or your building an API (no templates for APIs).
    That depends on what exactly you mean by an API: the standard generic algorithms can be said to form an API, and they certainly involve templates.

    Quote Originally Posted by Cogman
    Threading is another place where void*'s are pretty much unavoidable.
    Take a look at Boost.Thread, which is currently in the process of being adapted to be part of the C++ standard library.
    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

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    2,348
    Quote Originally Posted by iMalc View Post
    That's a terrble analogy Elkvis.

    You can't declare a void variable because it occupies no space. It would be like building a bridge out of a vacuum.
    oh come on, the analogy wasn't THAT bad

  14. #14
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Or your building an API (no templates for APIs).
    And/or you are using DLLs which generates warning C4251 when you attempt to export a template from a DLL. Templates are fine as long as they are internalized but exposing them to the outside world from an API introduces an entire plethora of issues.

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. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 08:45 PM

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