Structure function in C++

Hi, everybody,
I've been working through structures lately and must admit it's a hard work to use functions with them.Firstly I had some problems with declaring a structure with a function inside it. Secondly after I managed to solve this problem some new problems emerged inside main function.I wrote a piece of code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
 #include "IndexCombination.h"
#include <cstdlib>
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <map>
#include <sstream>
#include <mem.h>
#include <conio.h>
using namespace std;
using namespace stdcomb;

struct TestArray
{
       char name[6];
       int numbers[30];
       char chain[90];
       int find_combination(int TestArray[30],int);
           
}TestArray;

 
struct TestArray::TestArray(find_combination(int TestArray[30],int))
{int find_combination(int TestArray[30],int);}
    struct TestArray TestArray1 =        
        {
      "ia11",
      {3,9,17,21,24,31,33,36,42,49,4,8,19,22,28,30,34,39,43,47,2,6,10,13,14,25,29,37,38,46},
  "3,9,17,21,24,31,33,36,42,49,4,8,19,22,28,30,34,39,43,47,2,6,10,13,14,25,29,37,38,46" 
   }; 
   struct TestArray TestArray2[30]=
   {
      "ia34",
      {1,6,12,15,22,27,31,32,41,42,4,7,14,17,23,30,33,36,45,48,2,9,13,18,21,26,34,39,44,49},
     "1,6,12,15,22,27,31,32,41,42,4,7,14,17,23,30,33,36,45,48,2,9,13,18,21,26,34,39,44,49"
   };
    struct TestArray TestArray3[30]= 
   {
      "ia72",
       {3,8,11,18,22,25,36,37,43,46,1,6,16,17,23,28,35,40,41,44,2,5,12,19,24,27,31,34,42,49},
     "3,8,11,18,22,25,36,37,43,46,1,6,16,17,23,28,35,40,41,44,2,5,12,19,24,27,31,34,42,49"
   };
      struct TestArray TestArray4[30]= 
    {
      "ia167",
      {3,4,12,15,21,28,32,39,47,48,6,9,16,17,29,30,31,38,41,42,7,8,11,20,26,27,34,35,45,46},
    "3,4,12,15,21,28,32,39,47,48,6,9,16,17,29,30,31,38,41,42,7,8,11,20,26,27,34,35,45,46"
   };
   struct TestArray TestArray5[30]= 
    {
      "ia190",
       {3,10,11,14,22,23,32,35,43,44,6,9,16,19,24,29,37,38,45,48,2,5,13,18,25,30,31,40,41,0},
       "3,10,11,14,22,23,32,35,43,44,6,9,16,19,24,29,37,38,45,48,2,5,13,18,25,30,31,40,41,0"   
   };
   struct TestArray TestArray6[30]= 
    {
      "ia21",
      {2,5,16,19,25,30,34,39,45,48,1,9,12,13,21,24,33,36,44,49,3,8,15,20,23,26,35,40,43,46},
      "2,5,16,19,25,30,34,39,45,48,1,9,12,13,21,24,33,36,44,49,3,8,15,20,23,26,35,40,43,46"
   };
   struct TestArray TestArray7[30]= 
    {
      "ia64",
      {6,9,13,20,28,29,33,40,43,48,2,7,16,19,24,27,31,34,44,47,5,8,14,17,21,30,32,37,41,46},
     "6,9,13,20,28,29,33,40,43,48,2,7,16,19,24,27,31,34,44,47,5,8,14,17,21,30,32,37,41,46"
   };
   struct TestArray TestArray8[30]= 
    {
      "ia102",
      {4,9,14,19,22,29,35,38,46,47,5,8,13,16,21,26,33,40,41,48,6,7,15,17,28,30,32,39,42,45},
     "4,9,14,19,22,29,35,38,46,47,5,8,13,16,21,26,33,40,41,48,6,7,15,17,28,30,32,39,42,45"
   };
   struct TestArray TestArray9[30]= 
    {
      "ia178",
      {6,9,15,20,23,24,37,38,42,45,7,8,16,19,22,25,34,39,43,46,1,2,17,18,26,29,31,40,47,0},
      "6,9,15,20,23,24,37,38,42,45,7,8,16,19,22,25,34,39,43,46,1,2,17,18,26,29,31,40,47,0"
   };
   struct TestArray TestArray10[30]= 
    {
      "ia180",
      {1,4,13,18,27,30,33,38,45,46,2,3,12,15,22,23,39,40,44,47,8,9,11,17,24,25,32,37,49,0},
      "1,4,13,18,27,30,33,38,45,46,2,3,12,15,22,23,39,40,44,47,8,9,11,17,24,25,32,37,49,0"
      };
 
int main(int argc, char *argv[])

      
 {  
   CIdxComb cb;
   cb.SetSizes(10,6);
    
   vector<unsigned int> combination(6);
   combination[0] = 0;
   combination[1] = 1;
   combination[2] = 2;
   combination[3] = 3;
   combination[4] = 4;
   combination[5] = 5;

    int Total = 0;
  for
  (
 int find_combination(int TestArray[30],int);
int struct TestArray::TestArray(find_combination(int TestArray[30],int))//some errors
          
       do {      
             Total++;
             cout << "Combination number. " << Total  << endl;
             for (unsigned i = 0; i < combination.size(); i++)
                 {
                                        
                 const TestArray &numbers = numbers[combination[i]];
                 cout << numbers.name << ": " << numbers.chain << endl;                
                 }
                           
       while(cb.GetNextComb(combination));
}

    cout << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

and got an error saying:
C:\Dev-Cpp\main82.cpp In function `int main(int, char**)':
106 C:\Dev-Cpp\main82.cpp expected primary-expression before "int"
106 C:\Dev-Cpp\main82.cpp expected `;' before "int"
106 C:\Dev-Cpp\main82.cpp expected primary-expression before "int"
106 C:\Dev-Cpp\main82.cpp expected `)' before "int"
106 C:\Dev-Cpp\main82.cpp expected primary-expression before "int"
106 C:\Dev-Cpp\main82.cpp expected `;' before "int"
C:\Dev-Cpp\Makefile.win [Build Error] [main82.o] Error 1

I think there might be something wrong with braces and I've no idea what primary-expression before int may mean. The code isn't long. If somebody could have a look at it, I would be grateful.
Last edited on
You should: indent your code in a better way, write a constructor for your struct, change compiler
In addition to the things Bazzy notes:
The arguments for main are char** (array of c-style strings) not char*. Char*[] should be equal syntax but just go with the norm. If you aren't using commandline arguments just leave the brackets blank.
What the h*** does int struct mean? What are you putting in that for loop?
And lastly, I'm going to assume that on line 24 you are defining your member function, a task you have completely misinterpreted.
First of all you do not reuse the keyword struct. You start with the return type, then the scope and then the name, and then the arguments. In addition, a structure/class is a TYPE. You do not declare it with an integer unless you have created a template. In addition the compiler completely ignores the number you put in the subscripts of an argument unless that argument is a reference. So you should (I think) have something like this:
int TestArray::find_combination(TestArray*, int)
I can't even figure out the code in the function so rather than writing that gibberish, tell us what it does and we'll try to correct the various misinterpretations you have. I would also suggest that you go over your learnings on classes again and review that information. I don't mean to sound rude but you seem to be seriously misinterpreting your OOP.
tummychow wrote:
The arguments for main are char** (array of c-style strings) not char*. Char*[] should be equal syntax but just go with the norm.

OP has a correct main() declaration. char *argv[] equals char **argv. char*[] isn't even valid. It's actually valid, I confused it with char argv*[], but anyway char *argv[]==char **argv.
Last edited on
I reviewed some old code, R0mai, and you're right. But I persist in saying that, if you are not calling from command-line, it's easier to just go without the command line arguments.
By the way, to the original poster, several includes in your program can be sliced off: conio (which is nonstandard, all users beware), map, sstream, algorithm, iterator (I think) and maybe even string. Unnecessary headers cause code bloat and slow down your compilation.
Last edited on
What the h*** does int struct mean?
int struct TestArray::TestArray() Unless my precedence is messed up, that's a constructor for 'struct TestArray' ('struct T' and 'T' are synonymous. See below) that returns an int.
Although I'm unclear whether OP is declaring a constructor or calling a constructor. The latter would be illegal.


First of all you do not reuse the keyword struct.
The compiler ignores the "struct" part in 'struct T'. That's a hangover from C, where the keyword wasn't optional.

In addition the compiler completely ignores the number you put in the subscripts of an argument unless that argument is a reference.
True, but it's useful as a sort of inline documentation. Otherwise, someone using the function has no way of knowing whether the function takes a pointer to a variable or an array, or what the size of the array should be. Other than looking at the function, that is.

But I persist in saying that, if you are not calling from command-line, it's easier to just go without the command line arguments.
That can't be known at compile time. Many programs that aren't normally called from command line take command line arguments because that's not the only way of passing them.
The correct statement would be "if the program doesn't use command line arguments argc and argv are unnecessary."
Still, it doesn't hurt to leave them there.

Unnecessary headers cause code bloat and slow down your compilation.
They do slow down compilation, but they certainly don't cause bloat. It's the zero overhead principle: you don't pay for what you don't use. Templates only generate code if they are used (e.g. when creating an instance of an std::vector<int>). If a template is defined, but not used, it generates no code.


You don't have to get all flared up because someone is using syntax you don't know. C++ is a very, very large language.
Last edited on
You can declare a constructor with a return?
Besides it could not be a constructor because the OP did not put a constructor in the class definition. That would still cause an error. And is the struct keyword still ignored in line 24? I am pretty sure it is ignored in declaration of an object of an already-defined class but what about in the (attempted) function definition?
Last edited on
You can declare a constructor with a return?
Good point. What was I thinking? I have no idea what that line is supposed to be, then.

And is the struct keyword still ignored in line 24?
Hm. I just tested it and I was wrong. Methods have to be defined as return_type T::name(parameters){ body; }. struct is not allowed.
Your primary problem valdinez is you're trying to define 'find_combination' in the body of your main function. You cannot embed one function definition inside another.

Using methods(functions) with structs is done in the same manner as using methods with classes. The primary difference between structs and classes in C++ (outside the aspects necessary to maintain compatibility with the old C style structs), is that the default access type in a struct is public, where the default acces type for classes is private.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct mystruct{
	int a;
	char b;
	void setvals(int, char);
};

void mystruct::setvals(int x, char y){
	a = x;
	b = y;
}

mystruct q;
q.setvals(5, 'n');
cout << q.a << " " << q.b << endl;


You can even create private and protected sections in structs. In fact, you can do pretty much everything with structs that you can do with classes. Constructors/Destructors. Inheritance. Operator overloading. Etc.
Topic archived. No new replies allowed.