Thread: Errors, with vectors, windows programming... etc

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    141

    Errors, with vectors, windows programming... etc

    Code:
    #include <windows.h>
    #include <vector>
    #define ourKey VK_CONTROL
    typedef std:: vector< char > file;
    
    file.push_back('myfile.txt');
    file.push_back('myfile2.txt');
    file.push_back('pop_c.txt');
    
    int i=0;
    
    int main ( )
    {
    	
    
       if ( GetAsyncKeyState( ourKey ) <= -32767 )
       {
    
    	   bool KeyPress = true;
    
       } else bool KeyPress = false;
    
       if ( KeyPress == false ) keybd_event( ourKey, );
    
    	for ( i; i<=file.size(); i++ )
    	{
    
    		CreateFile(  file[i] , 
    					GENERIC_WRITE, 
    					0x00000002,  
    					NULL,
    					2,
    					0x80,
    					NULL );
    	}
       
    
       
    }
    Errors:
    Code:
    (5) : error C2143: syntax error : missing ';' before '.'
    (5) : error C2059: syntax error : '.'
    (5) : error C2015: too many characters in constant
    (6) : error C2143: syntax error : missing ';' before '.'
    (6) : error C2059: syntax error : '.'
    (6) : error C2015: too many characters in constant
    (7) : error C2143: syntax error : missing ';' before '.'
    (7) : error C2059: syntax error : '.'
    (7) : error C2015: too many characters in constant
    (16) : error C2275: 'file' : illegal use of this type as an expression
    1>        (3) : see declaration of 'file'
    (19) : error C2275: 'file' : illegal use of this type as an expression
    1>        (3) : see declaration of 'file'
    Dunno what happened, :S can someone help?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > typedef std:: vector< char > file;
    Use a std::string, not a char

    > file.push_back('myfile.txt');
    Use "double" quotes, not 'single'

    > int i=0;
    ... a lot of lines
    > for ( i;
    This is just an invitation for a lot of later grief. Where is it declared, and is it initialised?
    Use
    for ( int i = 0

    > CreateFile( file[i]
    Having changed to a std::string, you'll need file[i].c_str()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by Salem View Post
    > typedef std:: vector< char > file;
    Use a std::string, not a char

    > file.push_back('myfile.txt');
    Use "double" quotes, not 'single'

    > int i=0;
    ... a lot of lines
    > for ( i;
    This is just an invitation for a lot of later grief. Where is it declared, and is it initialised?
    Use
    for ( int i = 0

    > CreateFile( file[i]
    Having changed to a std::string, you'll need file[i].c_str()
    Thanks, I'll try it.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    141
    new errors:
    Code:
    (6) : error C2143: syntax error : missing ';' before '.'
    (6) : error C2059: syntax error : '.'
    (22) : error C2275: 'file' : illegal use of this type as an expression
    1>        (4) : see declaration of 'file'
    (25) : error C2275: 'file' : illegal use of this type as an expression
    1>        (4) : see declaration of 'file'
    (25) : error C2228: left of '.c_str' must have class/struct/union
    I removed the keybd_event till I could come back to it.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post your latest code
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by Salem View Post
    Post your latest code
    Code:
    #include <windows.h>
    #include <vector>
    #include <string>
    #define ourKey VK_CONTROL
    typedef std:: vector< std::string > file;
    
    file.push_back("myfile.txt");
    
    
    int main ( )
    {
    	
    
       if ( GetAsyncKeyState( ourKey ) <= -32767 )
       {
    
    	   bool KeyPress = true;
    
       } else bool KeyPress = false;
    
    
    
    	for ( int i = 0; i<=file.size(); i++ )
    	{
    
    		CreateFile(  file[i].c_str() , 
    					GENERIC_WRITE, 
    					0x00000002,  
    					NULL,
    					2,
    					0x80,
    					NULL );
    	}
       
    
       
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > file.push_back("myfile.txt");
    Put this inside main() perhaps?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    The error is that you don't instantiate an object of your 'vector<char>', but that you just give this specialisation ('char') of the template 'vector' an alias: typedef.
    You should instantiate an object before you can call member functions like "push_back" :
    Code:
    typedef std:: vector<char> file; // file is not the object, but a rename
    file myfile; // myfile is now the object
    myfile.push_back(..)
    Second, your loop loops one time too much: try
    Code:
    	
    for ( int i = 0; i<file.size(); i++ ) // no = !!
    If you count from 0, a vector with N elements has 0..N-1 entries.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by MarkZWEERS View Post
    The error is that you don't instantiate an object of your 'vector<char>', but that you just give this specialisation ('char') of the template 'vector' an alias: typedef.
    You should instantiate an object before you can call member functions like "push_back" :
    Code:
    typedef std:: vector<char> file; // file is not the object, but a rename
    file myfile; // myfile is now the object
    myfile.push_back(..)
    Second, your loop loops one time too much: try
    Code:
    	
    for ( int i = 0; i<file.size(); i++ ) // no = !!
    If you count from 0, a vector with N elements has 0..N-1 entries.
    But I don't understand, I'm not using frstream/ofstream/etc for my files, I'm using windows/com CreateFile function, the vector is just used so I can store an infinite amount of elements ( file names ), so I can create unlimited files from how many push_backs()'s I have, using for loop.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As Mark tries to explain to you:
    A typedef is an alias. You are creating an alias of the name of the class. A class is a blueprint; not an object. You cannot work on things that are not objects.
    So you must create an object of the type first.

    Try
    std::vector<std::string> file;

    This will work because it actually creates a vector of strings.

    This
    typedef std::vector<std::string> file;

    Merely says that you can now type "file" instead of "std::vector<std::string>". An alias.
    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.

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    141
    It works none the less, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  2. Linker Errors
    By beene in forum Game Programming
    Replies: 6
    Last Post: 11-18-2006, 12:09 PM
  3. Network Programming in C for Windows
    By m.mixon in forum C Programming
    Replies: 7
    Last Post: 06-19-2006, 08:27 PM
  4. Odd memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 05-25-2006, 12:56 AM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM