Thread: Vectors work when they want to.

  1. #16
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by medievalelks View Post
    Post the complete piece of code you're trying to compile, not just snippets. And if you're copy-pasting code, you have to be careful that you don't get any hidden HTML chars in it.
    Code:
    #include <vector>
    #include <iostream>
    std::vector<int> vec;
    vec.push_back(1);
    
    /*
    Errors:
    
    1>(4) : error C2143: syntax error : missing ';' before '.'
    1>(4) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>(4) : error C2371: 'vec' : redefinition; different basic types
    1>        (3) : see declaration of 'vec'
    
    */

  2. #17
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Ehm.. have you created a function 'int main()' ?

  3. #18
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    This works fine on VC++

    Code:
    #include <iostream>
    #include <vector>
    
    int main()
    {
       std:: vector<int> vec;
    
       vec.push_back(12);
       
       for ( int i = 0; i < vec.size(); i++ )
       {
           std::cout << vec[ i ] << " ";
       }
       
       std::cin.get();
       
       return 0;
    }
    Output is 12 as it should be
    Double Helix STL

  4. #19
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by swgh View Post
    This works fine on VC++

    Code:
    #include <iostream>
    #include <vector>
    
    int main()
    {
       std:: vector<int> vec;
    
       vec.push_back(12);
       
       for ( int i = 0; i < vec.size(); i++ )
       {
           std::cout << vec[ i ] << " ";
       }
       
       std::cin.get();
       
       return 0;
    }
    Output is 12 as it should be
    Well the int main made it work, but I've had it where it works without the int main, what is wrong with C++???

    Proof it works without int main:

    http://cboard.cprogramming.com/showthread.php?t=105607

  5. #20
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    What do you expect your computer to execute when there is no function 'main' ? There is something wrong with your code and your knowledge of C++, not of C++ itself..

  6. #21
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Every single C.C++ program needs a main function. It is the starting location and the first function called by the operating system. 99&#37; of the time no C++ program will compile without a main function. The OS needs to know where to start from.
    Double Helix STL

  7. #22
    Registered User
    Join Date
    May 2008
    Posts
    141
    Maybe it's not weither it's in the scope or not, but weither the int main is even called.

    Edit: doesn't work tohugh, wtf????

  8. #23
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by swgh View Post
    Every single C.C++ program needs a main function. It is the starting location and the first function called by the operating system. 99% of the time no C++ program will compile without a main function. The OS needs to know where to start from.
    Even the big programs? Like Firefox?

  9. #24
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Why are you trying to create a program without main()? Its bad practice anyway, and all good up to date compilers will forbid it unless you have some special setting or circumstance on when main will not be called
    Double Helix STL

  10. #25
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    You can also just create shared object libraries (.so , Unix/Linux), or dynamic link libraries (.dll , windowz) , which are loaded in some platform or by another program. But then, that program does have a function main.

    No main no brain

  11. #26
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by swgh View Post
    Why are you trying to create a program without main()? Its bad practice anyway, and all good up to date compilers will forbid it unless you have some special setting or circumstance on when main will not be called
    It's not that, it's just that it seems I get errors when I try to do stuff outside the int main, go check out my other thread about vectors and windows, I have the vector outside the main, and after I fixed it up from the other errors, it worked fine outside the int main, so whats the deal?

  12. #27
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    It could have been defined in the scope of a free function and compiled with -c - i.e., no main() function needed.

    But to the OP, how and when did you expect that code to get executed if it wasn't in main() or any other function?

  13. #28
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by medievalelks View Post
    It could have been defined in the scope of a free function and compiled with -c - i.e., no main() function needed.

    But to the OP, how and when did you expect that code to get executed if it wasn't in main() or any other function?
    Well I know int main is the main ( -_- ) function that executes it all, but I didn't know it is that strict.

  14. #29
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    The C++ standard has many rules and definitions over main. Main is like the heart of a program. It can ( and somtimes does ) call a single function. But without it, nothing would work. Remember, unless told otherwise by a called function, a program will also terminate via main's return statement.
    Double Helix STL

  15. #30
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by swgh View Post
    The C++ standard has many rules and definitions over main. Main is like the heart of a program. It can ( and somtimes does ) call a single function. But without it, nothing would work. Remember, unless told otherwise by a called function, a program will also terminate via main's return statement.
    Okay. So why does the main be an int? why not.. char?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  3. Adding vectors ??
    By Hussain Hani in forum C++ Programming
    Replies: 1
    Last Post: 11-18-2006, 03:03 AM
  4. Axis based on 2 Vectors
    By durban in forum Game Programming
    Replies: 1
    Last Post: 11-10-2005, 04:38 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM