Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. [newbie] error C2065: 'b' : undeclared identifier

[newbie] error C2065: 'b' : undeclared identifier

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++visual-studiodata-structureshelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jon 80
    wrote on last edited by
    #1

    Any idea why this code doesn't compile?

    // SieveOfErathostenes.cpp : main project file.

    #include "stdafx.h"
    #include <bitset>
    #include <iostream>
    #include <ctime>

    using namespace System;

    int main(array<System::String ^> ^args)
    {
    const int N = 2000000;
    clock_t cstart = clock();

    bitset<N + 1> b;
    int count = 0;
    int i;
    for (i = 2; i <= N; i++)
        b.set(i);
    i = 2;
    
    while (i \* i <= N)
    {
        if (b.test(i))
        {
            count++;
            int k = 2 \* i;
            while (k <= N)
            {
                b.reset(k);
                k += i;
            }
        }
        i++;
    }
    
    while (i <= N)
    {
        if (b.test(i))  count++;
        i++;
    }
    
    clock\_t cend = clock();
    double millis = 1000.0 \* (cend - cstart) / CLOCKS\_PER\_SEC;
    
    cout << count << " primes \\n" << millis << " milliseconds\\n";
    
    return 0;
    

    }

    Errors: Error 4 error C2065: 'b' : undeclared identifier c:\Users\Administrator\Documents\Visual Studio 2008\Projects\SieveOfErathostenes\SieveOfErathostenes\SieveOfErathostenes.cpp 19 SieveOfErathostenes Error 5 error C2228: left of '.set' must have class/struct/union c:\Users\Administrator\Documents\Visual Studio 2008\Projects\SieveOfErathostenes\SieveOfErathostenes\SieveOfErathostenes.cpp 19 SieveOfErathostenes Error 6 error C2065: 'b' : undeclared identifier c:\Users\Administrator\Documents\Visual Studio 2008\Projects\SieveOfErathostenes\SieveOfErathostenes\SieveOfErathostenes.cpp 24 SieveOfErathostenes Error 7 error C2228: left of '.test' must have class/struct/union c:\Users\Administrator\Documents\Visual Studio 2008\Projects\SieveOfErathostenes\SieveOfErathostenes\SieveOfErathostenes.cpp 24 SieveOfErathostenes Error 8 error C2065: 'b' : undeclared identifier c:\Users\Administrator\Documents\Visual Studio 2008\Projects\SieveOfErathostenes\SieveOfErathostenes\SieveOfErathostenes.cpp 30 SieveOfErathostenes Error 9 error C2228: left of '.reset' must have class/struct/union c:\Users\Administrator\Documents\Visual Studio 2008\Projects\SieveOfErathostenes\SieveOfErathostenes\SieveOfErathostenes.cpp 30 SieveOfErathostenes Error 10 error C2065: 'b' : undeclared identifier c:\Users\Administrator\Documents\Visual Studio 2008\Projects\SieveOfErathostenes\SieveOfErathostenes\SieveOfErathostenes.cpp 39 SieveOfErathostenes Error 11 error C2228: left of '.test' must have

    C S 2 Replies Last reply
    0
    • J jon 80

      Any idea why this code doesn't compile?

      // SieveOfErathostenes.cpp : main project file.

      #include "stdafx.h"
      #include <bitset>
      #include <iostream>
      #include <ctime>

      using namespace System;

      int main(array<System::String ^> ^args)
      {
      const int N = 2000000;
      clock_t cstart = clock();

      bitset<N + 1> b;
      int count = 0;
      int i;
      for (i = 2; i <= N; i++)
          b.set(i);
      i = 2;
      
      while (i \* i <= N)
      {
          if (b.test(i))
          {
              count++;
              int k = 2 \* i;
              while (k <= N)
              {
                  b.reset(k);
                  k += i;
              }
          }
          i++;
      }
      
      while (i <= N)
      {
          if (b.test(i))  count++;
          i++;
      }
      
      clock\_t cend = clock();
      double millis = 1000.0 \* (cend - cstart) / CLOCKS\_PER\_SEC;
      
      cout << count << " primes \\n" << millis << " milliseconds\\n";
      
      return 0;
      

      }

      Errors: Error 4 error C2065: 'b' : undeclared identifier c:\Users\Administrator\Documents\Visual Studio 2008\Projects\SieveOfErathostenes\SieveOfErathostenes\SieveOfErathostenes.cpp 19 SieveOfErathostenes Error 5 error C2228: left of '.set' must have class/struct/union c:\Users\Administrator\Documents\Visual Studio 2008\Projects\SieveOfErathostenes\SieveOfErathostenes\SieveOfErathostenes.cpp 19 SieveOfErathostenes Error 6 error C2065: 'b' : undeclared identifier c:\Users\Administrator\Documents\Visual Studio 2008\Projects\SieveOfErathostenes\SieveOfErathostenes\SieveOfErathostenes.cpp 24 SieveOfErathostenes Error 7 error C2228: left of '.test' must have class/struct/union c:\Users\Administrator\Documents\Visual Studio 2008\Projects\SieveOfErathostenes\SieveOfErathostenes\SieveOfErathostenes.cpp 24 SieveOfErathostenes Error 8 error C2065: 'b' : undeclared identifier c:\Users\Administrator\Documents\Visual Studio 2008\Projects\SieveOfErathostenes\SieveOfErathostenes\SieveOfErathostenes.cpp 30 SieveOfErathostenes Error 9 error C2228: left of '.reset' must have class/struct/union c:\Users\Administrator\Documents\Visual Studio 2008\Projects\SieveOfErathostenes\SieveOfErathostenes\SieveOfErathostenes.cpp 30 SieveOfErathostenes Error 10 error C2065: 'b' : undeclared identifier c:\Users\Administrator\Documents\Visual Studio 2008\Projects\SieveOfErathostenes\SieveOfErathostenes\SieveOfErathostenes.cpp 39 SieveOfErathostenes Error 11 error C2228: left of '.test' must have

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      Probably you get errors because are in the wrong forum (better luck here [^], maybe). :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      1 Reply Last reply
      0
      • J jon 80

        Any idea why this code doesn't compile?

        // SieveOfErathostenes.cpp : main project file.

        #include "stdafx.h"
        #include <bitset>
        #include <iostream>
        #include <ctime>

        using namespace System;

        int main(array<System::String ^> ^args)
        {
        const int N = 2000000;
        clock_t cstart = clock();

        bitset<N + 1> b;
        int count = 0;
        int i;
        for (i = 2; i <= N; i++)
            b.set(i);
        i = 2;
        
        while (i \* i <= N)
        {
            if (b.test(i))
            {
                count++;
                int k = 2 \* i;
                while (k <= N)
                {
                    b.reset(k);
                    k += i;
                }
            }
            i++;
        }
        
        while (i <= N)
        {
            if (b.test(i))  count++;
            i++;
        }
        
        clock\_t cend = clock();
        double millis = 1000.0 \* (cend - cstart) / CLOCKS\_PER\_SEC;
        
        cout << count << " primes \\n" << millis << " milliseconds\\n";
        
        return 0;
        

        }

        Errors: Error 4 error C2065: 'b' : undeclared identifier c:\Users\Administrator\Documents\Visual Studio 2008\Projects\SieveOfErathostenes\SieveOfErathostenes\SieveOfErathostenes.cpp 19 SieveOfErathostenes Error 5 error C2228: left of '.set' must have class/struct/union c:\Users\Administrator\Documents\Visual Studio 2008\Projects\SieveOfErathostenes\SieveOfErathostenes\SieveOfErathostenes.cpp 19 SieveOfErathostenes Error 6 error C2065: 'b' : undeclared identifier c:\Users\Administrator\Documents\Visual Studio 2008\Projects\SieveOfErathostenes\SieveOfErathostenes\SieveOfErathostenes.cpp 24 SieveOfErathostenes Error 7 error C2228: left of '.test' must have class/struct/union c:\Users\Administrator\Documents\Visual Studio 2008\Projects\SieveOfErathostenes\SieveOfErathostenes\SieveOfErathostenes.cpp 24 SieveOfErathostenes Error 8 error C2065: 'b' : undeclared identifier c:\Users\Administrator\Documents\Visual Studio 2008\Projects\SieveOfErathostenes\SieveOfErathostenes\SieveOfErathostenes.cpp 30 SieveOfErathostenes Error 9 error C2228: left of '.reset' must have class/struct/union c:\Users\Administrator\Documents\Visual Studio 2008\Projects\SieveOfErathostenes\SieveOfErathostenes\SieveOfErathostenes.cpp 30 SieveOfErathostenes Error 10 error C2065: 'b' : undeclared identifier c:\Users\Administrator\Documents\Visual Studio 2008\Projects\SieveOfErathostenes\SieveOfErathostenes\SieveOfErathostenes.cpp 39 SieveOfErathostenes Error 11 error C2228: left of '.test' must have

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        The first error I get is this:

        a.cpp(14) : error C2065: 'bitset' : undeclared identifier

        This is because you use bitset without prefixing it with std::, which is the namespace it lives in. Similarly, cout needs a std:: prefix, because that's where it lives. [edit]Fix those two, and your code compiles fine. It runs as well.[/edit]

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        J 1 Reply Last reply
        0
        • S Stuart Dootson

          The first error I get is this:

          a.cpp(14) : error C2065: 'bitset' : undeclared identifier

          This is because you use bitset without prefixing it with std::, which is the namespace it lives in. Similarly, cout needs a std:: prefix, because that's where it lives. [edit]Fix those two, and your code compiles fine. It runs as well.[/edit]

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          J Offline
          J Offline
          jon 80
          wrote on last edited by
          #4

          Resolved that by including 'using namespace std;'. Now it reads: C1010 - unexpected eof. Says it needs a header file, but I "forgot" what to include. Afiak the header file includes: #pragma once ... ...and the headers of the methods used in my case... int main... However, it's strange that the default template created by visual studio with a main method did not require a .header file.

          // SieveOfErathostenes.cpp : main project file.

          #include <bitset>
          #include <iostream>
          #include <ctime>

          using namespace System;
          using namespace std;

          int main(array<System::String ^> ^args)
          {
          const int N = 2000000;
          clock_t cstart = clock();

          std::bitset<N + 1> b;
          int count = 0;
          int i;
          for (i = 2; i <= N; i++)
              b.set(i);
          i = 2;
          
          while (i \* i <= N)
          {
              if (b.test(i))
              {
                  count++;
                  int k = 2 \* i;
                  while (k <= N)
                  {
                      b.reset(k);
                      k += i;
                  }
              }
              i++;
          }
          
          while (i <= N)
          {
              if (b.test(i))  count++;
              i++;
          }
          
          clock\_t cend = clock();
          double millis = 1000.0 \* (cend - cstart) / CLOCKS\_PER\_SEC;
          
          cout << count << " primes \\n" << millis << " milliseconds\\n";
          
          return 0;
          

          }

          Sieve.h
          #pragma once
          #include <iostream>

          int main(array<System::String ^> ^args);

          Related links: C++ Header files[^]

          Jon

          S 1 Reply Last reply
          0
          • J jon 80

            Resolved that by including 'using namespace std;'. Now it reads: C1010 - unexpected eof. Says it needs a header file, but I "forgot" what to include. Afiak the header file includes: #pragma once ... ...and the headers of the methods used in my case... int main... However, it's strange that the default template created by visual studio with a main method did not require a .header file.

            // SieveOfErathostenes.cpp : main project file.

            #include <bitset>
            #include <iostream>
            #include <ctime>

            using namespace System;
            using namespace std;

            int main(array<System::String ^> ^args)
            {
            const int N = 2000000;
            clock_t cstart = clock();

            std::bitset<N + 1> b;
            int count = 0;
            int i;
            for (i = 2; i <= N; i++)
                b.set(i);
            i = 2;
            
            while (i \* i <= N)
            {
                if (b.test(i))
                {
                    count++;
                    int k = 2 \* i;
                    while (k <= N)
                    {
                        b.reset(k);
                        k += i;
                    }
                }
                i++;
            }
            
            while (i <= N)
            {
                if (b.test(i))  count++;
                i++;
            }
            
            clock\_t cend = clock();
            double millis = 1000.0 \* (cend - cstart) / CLOCKS\_PER\_SEC;
            
            cout << count << " primes \\n" << millis << " milliseconds\\n";
            
            return 0;
            

            }

            Sieve.h
            #pragma once
            #include <iostream>

            int main(array<System::String ^> ^args);

            Related links: C++ Header files[^]

            Jon

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #5

            jon_80 wrote:

            C1010 - unexpected eof. Says it needs a header file, but I "forgot" what to include. Afiak the header file includes:

            stdafx.h - it's probably got pre-compiled headers enabled if it's a VS project. You don't need the Sieve.h file.

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups