error in auto

I would like to fix this code:

#include<iostream>
#include<vector> // Standard-Vektor
#include<string> // Standard-String
#include<cstddef> // size_t
using namespace std;

struct Punkt {
int x;
int y;
};

int main() {
Punkt p1 = { 100, 200 };
auto p2 = p1;
cout << "p2.x= " << p2.x << " p2.y= " << p2.y << endl;

vector<double> v1 = {1.1, 2.2, 3.3, 4.4, 5.5};
auto v2 = v1;
for(size_t i = 0; i < v2.size(); ++i) {
cout << i << ": " << v2[i] << endl;
}

string s1("Ende!");
auto s2 = s1;
cout << s2 << endl;
}

There is an error for auto
What's the error? Does your compiler support C++11 auto?
If you are using gcc, did you turn on C++11 support in compileer options?
I use codeBlocks the latest 12.11. Where is to turn, I really new here.
Settings → Compiler and Debugger → Have g++ follow the coming C++0x ISO C++ language standart
I think: build options -> other options (insted of compiler flags): -std=c++11

But I think you need gcc 4.7 for this (4.7.2 is out)
Last edited on
MiiNiiPaa-
I use your step, the error is reduced,
Do you use CodeBlock 12.11, maybe you will find out the problems

Here is the error now:

||=== L1p18st90, Debug ===|
C:\GERMANY2012\OpenSourceSoftware2012\CodeBlockCPP\cpp_projekt\L1p18st90\main.cpp||In function 'int main()':|
C:\GERMANY2012\OpenSourceSoftware2012\CodeBlockCPP\cpp_projekt\L1p18st90\main.cpp|21|error: conflicting declaration 'auto p2'|
C:\GERMANY2012\OpenSourceSoftware2012\CodeBlockCPP\cpp_projekt\L1p18st90\main.cpp|18|error: 'p2' has a previous declaration as 'int p2'|
C:\GERMANY2012\OpenSourceSoftware2012\CodeBlockCPP\cpp_projekt\L1p18st90\main.cpp|22|error: request for member 'x' in 'p2', which is of non-class type 'int'|
C:\GERMANY2012\OpenSourceSoftware2012\CodeBlockCPP\cpp_projekt\L1p18st90\main.cpp|22|error: request for member 'y' in 'p2', which is of non-class type 'int'|
||=== Build finished: 4 errors, 0 warnings (0 minutes, 0 seconds) ===|
Your code compiles just fine for me. Do you tried to fix problem yourself and accidenly redeclared p2?

Code::Blocks gives you line numbers in error messages. Look for this lines and try to find error there.

Also you don't need to include cstddef header for size_t in C++11.
Yes the problem fixed. Thanks a lot.
MiiNiPaa wrote:
Also you don't need to include cstddef header for size_t in C++11.

You need to include a header that defines size_t, or define it yourself, before you can use it. Often standard headers include other headers so there is a big chance that if you include a standard header you will have size_t defined because it is such a commonly used type, but there is no guarantee and this can differ between different implementations and even between different version of the same implementation, so to be safe it is best to always include the headers that defines the things you are using.
Last edited on
Topic archived. No new replies allowed.