[newbie] error C2065: 'b' : undeclared identifier
-
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
-
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
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] -
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
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
-
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
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
-
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
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