-
May 1st, 2010, 02:42 PM
#1
Bunch of errors.
Whats wrong with this code:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
struct rec {
int height;
int width;
};
vector<struct rec[4]> gen(struct rec arr []){
vector<struct rec[4]> generated;
for(int a = 0; a < 4; a++){
for(int b = 0; b < 4; b++){
if(b != a){
for(int c = 0; c < 4; c++){
if(c!=a && c!= b){
for(int d = 0; d < 4; d++){
if(d!=a&&d!=b&&d!=c){
struct rec array [4];
array[0] = arr[a];
array[1] = arr[b];
array[2] = arr[c];
array[3] = arr[d];
generated.push_back(array);
}
}
}
}
}
}
}
return generated;
}
int main(){
}
I get almost a screen-full of errors when I try to compile this in the terminal. Any help would be greatly appreciated. Thanks in advance
-
May 1st, 2010, 03:23 PM
#2
Re: Bunch of errors.
You cant have a vector of array types like that. The type held by the vector must be copyable and assignable.
Whats wrong with this code:
Its not tagged!
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
May 1st, 2010, 07:09 PM
#3
Re: Bunch of errors.
Originally Posted by dietao234
Whats wrong with this code:
The code is a combination of an attempt of C++, mixed with 'C'. Regardless, it's wrong (and it wasn't placed in code tags).
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
#include <array>
using namespace std;
struct rec
{
int height;
int width;
};
typedef tr1::array<rec, 4> Rec4;
typedef std::vector<Rec4> Rec4Vector;
Rec4Vector gen(rec arr [])
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
struct rec
{
int height;
int width;
};
struct Rec4
{
rec recArray[4];
};
typedef std::vector<Rec4> Rec4Vector;
Rec4Vector gen(rec arr [])
Take your pick. If you have the tr1::array class (or equivalent array template class), use the first example.
If you don't have the class, then fake it out by creating a struct that contains an array of 4 elements (and change your code accordingly). That is what the second example represents.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; May 1st, 2010 at 07:49 PM.
-
May 1st, 2010, 07:39 PM
#4
Re: Bunch of errors.
Given the nature of your four-deep-for-loop, you may want to look into algorithm std::next_permutation. It should generate the same arrays you are getting with your loops.
Also, consider using "continue" keyword, instead of nesting your ifs, you'll get smaller and more manageable code:
Code:
//Your loop
for(int a = 0; a < 4; a++){
for(int b = 0; b < 4; b++){
if(b != a){
for(int c = 0; c < 4; c++){
if(c!=a && c!= b){
for(int d = 0; d < 4; d++){
if(d!=a&&d!=b&&d!=c){
struct rec array [4];
array[0] = arr[a];
array[1] = arr[b];
array[2] = arr[c];
array[3] = arr[d];
generated.push_back(array);
}
}
}
}
}
}
}
//Your loop using continue
for(int a = 0; a < 4; a++){
for(int b = 0; b < 4; b++){
if(b == a){ continue }
for(int c = 0; c < 4; c++){
if(c==a || c== b){ continue }
for(int d = 0; d < 4; d++){
if(d==a || d==b || d==c){ continue }
struct rec array [4];
array[0] = arr[a];
array[1] = arr[b];
array[2] = arr[c];
array[3] = arr[d];
generated.push_back(array);
}
}
}
}
//using next_permutation
int index[4] = {0,1,2,3};
int* beg = index;
int* end = index+4;
do {
struct Rec4 array;
for(int i = 1; i<4; ++i)
{
array.rec[i] = arr.rec[index[i]];
}
generated.push_back(array);
} while ( next_permutation(beg,end) );
Last edited by monarch_dodra; May 1st, 2010 at 07:42 PM.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|