User forums > General (but related to Code::Blocks)
Weird errors? C++
(1/1)
npro:
Hello,
I just installed Code::Blocks and I use the GNU GCC Compiler. Then I tried to add some working code(tested in visual studio successfully), and the code was:
--- Code: ---// overloading operators example
#include <iostream>
#include <string>
using namespace std;
int nopel;
class ArrayWrapper
{
public:
// default constructor produces a moderately sized array
ArrayWrapper()
: _p_vals(new int[64])
{
std::cout << "in arraywrapper constructor no arg";
}
ArrayWrapper(int n)
: _p_vals(new int[n])
{
std::cout << "in arraywrapper constructor args";
}
// move constructor
ArrayWrapper(ArrayWrapper&& other)
: _p_vals(other._p_vals)
{
std::cout << "in arraywrapper move constructor";
other._p_vals = NULL;
}
// copy constructor
ArrayWrapper(const ArrayWrapper& other)
: _p_vals(new int[5])
{
std::cout << "in arraywrapper copy constructor";
for (int i = 0; i < 6; ++i)
{
_p_vals[i] = other._p_vals[i];
}
}
~ArrayWrapper()
{
delete[] _p_vals;
}
private:
int *_p_vals;
};
void tryer(int&& i) { i = 12; }
int main() {
system("pause");
return 0;
}
--- End code ---
It all works just as expected in visual studio as mentioned before, but when I try to compile it in Code::Blocks, I get all these weird errors:
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\Users\ss\Documents\c++ files\tryy.cpp|29|error: expected ',' or '...' before '&&' token|
C:\Users\ss\Documents\c++ files\tryy.cpp|29|error: invalid constructor; you probably meant 'ArrayWrapper (const ArrayWrapper&)'|
C:\Users\ss\Documents\c++ files\tryy.cpp|57|error: expected ',' or '...' before '&&' token|
C:\Users\ss\Documents\c++ files\tryy.cpp||In function 'void tryer(int)':|
C:\Users\ss\Documents\c++ files\tryy.cpp|57|error: 'i' was not declared in this scope|
C:\Users\ss\Documents\c++ files\tryy.cpp||In function 'int main()':|
C:\Users\ss\Documents\c++ files\tryy.cpp|61|error: 'system' was not declared in this scope|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Can anybody please tell me how I can fix this? I mean it is pretty frustrating when it all should work and works in visual studio.
Thanks!!
BlueHazzard:
Codeblocks is a IDE, not a compiler. You get compiler errors so this is the wrong forum to ask this kind of questions. We support only codeblocks not the underlying compiler...
One thing i noted:
--- Quote ---C:\Users\ss\Documents\c++ files\tryy.cpp|29|error: expected ',' or '...' before '&&' token|
--- End quote ---
this comes probably from:
--- Code: ---ArrayWrapper(ArrayWrapper&& other)
--- End code ---
so you are using a new c++ feature. You need at least GCC 4.3 to use RValue reference and you will enable c++11 by adding the "-std=c++11" compiler flag to the build options
Krice:
Here's my thoughts about that code:
1. Don't make array classes yourself. Just don't. Use STL.
2. using namespace std; Don't write that line again ever. Use parts of STL if needed: using std::vector; for example.
3. Don't use global variables in C++ (that 'nopel' whatever it is, totally crappy name for a variable).
4. Don't use _ at front of your own variables.
5. Don't use magic numbers.
6. Your for-loop leaks, it's one off. It should be 0 to less than 5 (0-4 is 5 items).
7. Rather than programming try thinking some other things to do in life.
sodev:
--- Quote from: npro on July 09, 2017, 11:42:08 pm ---It all works just as expected in visual studio as mentioned before, but when I try to compile it in Code::Blocks, I get all these weird errors:
--- End quote ---
So it crashes on copy constructing an ArrayWrapper if the source has less than 6 elements and doesn't copy more than 5 elements of it? Well, despite you apparently don't know what you are coding there, the real problem is this:
--- Quote ---||=== Build file: "no target" in "no project" (compiler: unknown) ===|
--- End quote ---
CodeBlocks doesn't work well without a project, so first create a project (afaik Visual Studio doesn't work without projects at all, so why didnt you make a project here as well?!). Then you also might need to configure it to use the proper compiler and enable c++11 support in it (only very recent GCC should ship with c++11 enabled by default). But after all i've seen from you so far i think the easiest solution is to stay with Visual Studio.
Navigation
[0] Message Index
Go to full version