Thread: compilation error

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    92

    Lightbulb compilation error

    i am writing a program which will find a string from an array of strings.



    PHP Code:

    #include<iostream>
     #include<cstdlib>
     #include<string>
     #include<cmath>
     #include<vector>
     
    using namespace std;

     
    typedef  std::<vector std::stringstr;

     
    str s("abcd","efgh","ijkl","mnop","123"); //  string initialized

     
    int main()

     {

     
    string s1="ijkl"// target string

     
    std::vector<std::stringiterator p ;

    for(
    iterator p s.begin(); p!=s.end();p++)
    {

     if( 
    s1==s[p]) cout<<"string has been found"<<endl;  // string has been found

    }

    int n atoi("123");  // is it possible in C++ ?
    cout<<n<<endl;



    my errors >

    error C2589: '<' : illegal token on right side of '::'
    error C2589: '<' : illegal token on right side of '::'
    warning C4091: 'typedef ' : ignored on left of 'int' when no variable is declared
    error C2143: syntax error : missing ';' before '::'
    error C2143: syntax error : missing ';' before '::'
    error C2955: 'vector' : use of class template requires template argument list
    c:\program files\microsoft visual studio\vc98\include\vector(244) : see declaration of 'vector'
    error C2146: syntax error : missing ';' before identifier 's'
    error C2501: 'str' : missing storage-class or type specifiers
    fatal error C1004: unexpected end of file found
    Error executing cl.exe.


    why these errors ? how can i fix those errors ?
    thanks
    blue_gene

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    #include<iostream> 
    #include<cstdlib> 
    #include<string> 
    #include<cmath> 
    #include<vector> 
    using namespace std; 
    
    typedef  std::vector <std::string> str; 
    
    int main() 
    { 
      str s;
      s.push_back("abcd");
      s.push_back("efgh");
      s.push_back("ijkl");
      s.push_back("mnop");
      s.push_back("123");
      string s1="ijkl"; // Also unnecessary comment, use a better name
      std::vector<std::string>::iterator p; 
      for(p = s.begin(); p!=s.end();p++) 
      { 
    
        if( s1==*p) cout<<"string has been found"<<endl;  // Unnecessary comment
    
      } 
      int n = atoi("123");  // Yes, it is possible. :-)
      cout<<n<<endl; 
    }
    The more obvious errors are:

    >typedef std::<vector std::string> str;
    Misplaced opening angle bracket. It should be:
    Code:
    typedef  std::vector <std::string> str;
    >str s("abcd","efgh","ijkl","mnop","123"); // string initialized
    Sadly, you cannot initialize a vector like this.

    >std::vector<std::string> iterator p ;
    iterator is a data type of the vector class, use the scope resolution operator to access it:
    Code:
    std::vector<std::string>::iterator p;
    >for(iterator p = s.begin(); p!=s.end();p++)
    p already exists, you're trying to declare it again with an unknown type. This should be:
    Code:
    for(p = s.begin(); p!=s.end();p++)
    >if( s1==s[p])
    An iterator is like a pointer, not like an index. s[p] is an undefined operation. Instead, dereference the iterator like a pointer:
    Code:
    if( s1==*p)
    >int n = atoi("123"); // is it possible in C++ ?
    Yes it is possible, but calling atoi on a string literal directly serves no purpose. It would be easier and faster to simply do this:
    Code:
    int n = 123;
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    thanks prelude . i understood the errors. i have one more question.

    Code:
    #include<iostream>
     #include<cstdlib>
     #include<string>
     #include<cmath>
     #include<vector>
     using namespace std;
    
    
    
     int main()
    
     {
    
    
    
    string  s1 = "123";
    int n = atoi(s1);        // code-1  .....this code is giving compile error 
    
    cout<<n<<endl; 
    
    
    
    
    
    
    
    int n = atoi("123");     // code-2....... this code is ok
    cout<<n<<endl;
    
    
    
    }


    i want the code-1 ready... bcoz i want to store string in a variable and then want to use atoi() to get integers .


    how can i remove error from code-1 ?

    code-1 is giving error >

    cannot convert `std::string' to `const char*' for argument `1' to `
    int atoi(const char*)'

    (g++ compiler)


    what this error demands ?
    is it possible to remove error from the code-1 ?

    thanks
    blue_gene

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >int n = atoi(s1);
    s1 is a C++ string, but atoi takes a C-style string. To get a const char * from a C++ string, use the c_str member function:
    Code:
    int n = atoi(s1.c_str());
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM