Class instantiation and other errors

Am new to C++ and am trying to convert a little C program (which uses structs) to C++ using classes. To keep it simple it's a console application. My original uses Turbo C++ (yes, I know it's old) but I've also tried compiling with g++ (commenting out specific Turbo features like conio.h and gotoxy(), wherex() etc).

I'm getting a couple of errors (marked 1 and 2) in the code below.

Error 1:

C:\TCPP\WORKAREA\FORMTEST\MYFORM.CPP :In constructor 'Form::Form(Field*, short unsigned int, void*)':|
C:\TCPP\WORKAREA\FORMTEST\MYFORM.CPP :error: cannot convert 'Field' to 'Field*' in assignment|

Error 2:

C:\TCPP\WORKAREA\FORMTEST\MYFORM.CPP :In member function 'void Form::show()':
C:\TCPP\WORKAREA\FORMTEST\MYFORM.CPP :error: request for member 'fldPrompt' in '((Form*)this)->Form::fields[i]', which is of non-class type 'Field*'
C:\TCPP\WORKAREA\FORMTEST\MYFORM.CPP :error: request for member 'startRow' in '((Form*)this)->Form::fields[i]', which is of non-class type 'Field*'
C:\TCPP\WORKAREA\FORMTEST\MYFORM.CPP :error: request for member 'fldPrompt' in '((Form*)this)->Form::fields[i]', which is of non-class type 'Field*'
C:\TCPP\WORKAREA\FORMTEST\MYFORM.CPP :error: expected ';' before ')' token|
||=== Build finished: 5 errors, 0 warnings ===|

It's probably something quite basic but I'd appreciate a guiding hand! Any other constructive comments would also be appreciated.

Thanks

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
// myform.h : declaration of classes

#ifndef MYFORM_H
#define MYFORM_H

#define NUMFLDS  4
#define MAXFLDLEN 25

class Form;              // forward declaration

class Field {
private:
  char     *fldPrompt;
  unsigned short fldLen; // actual length without '\0' for char fields
  unsigned short startRow;
  unsigned short startCol;
public:
  Field(char *pmpt, unsigned short len, unsigned short row, unsigned short col);
  friend class Form;     // allow Form to access Field's private members
  ~Field();
};

class Form {
private:
  Field *fields[NUMFLDS];
  void  *data[NUMFLDS];
  unsigned short fldNums;
public:
  Form(Field *flds, unsigned short numFlds, void *dta);
 ~Form();
  void show(void);
  void process(void);
};

#endif

===

// myform.cpp
#include <stdio.h>
//#include <conio.h>
#include <string.h>
#include "myform.h"

// Field implementation

Field::Field(char *pmpt, unsigned short len, unsigned short row, unsigned short col)
{
  fldPrompt = pmpt;
  fldLen = len;
  startRow = row;
  startCol = col;
}

Form::Form(Field *flds, unsigned short numFlds, void *dta)
{
  fldNums = numFlds;
  for (int i = 0; i < fldNums; i++) {
   fields[i] = flds[i];                <<<<< ERROR 1
  }
  show();
  process();
}

void Form::show(void)
{
  for (int i = 0; i < fldNums; i++) {  // show the form
	//	gotoxy(fields[i].startCol,fields[i].startRow);  
		printf(fields[i].fldPrompt);                     <<<< ERROR 2
	//	gotoxy(fields[i].startCol,
		fields[i].startRow+strlen(fields[i].fldPrompt)); <<<< ERROR 2
  }
}

void Form::process(void)
{
  // commented out
}

===

// myfrmtst.cpp

#include <stdio.h>
//#include <conio.h>
#include "myform.h"

const int numfields = 4;

/******************************* data ********************************/

char firstName[13]; /* allow for terminating '\0' in input bufs */
char surName[26];
char gender[2];
char age[3];

int main(void)
{
  Field *fld[] = {
                  &Field("First Name : ", 12, 4, 17),
                  &Field("Surname    : ", 25, 6, 17),
                  &Field("Gender     : ",  1, 8, 17),
                  &Field("Age        : ",  2, 10,17)
                };

  void  *dat[] = { &firstName, &surName, &gender, &age };

  Form  frm(*fld, numfields, *dat);


//  getch();
  return 0;
}


Error 1: Fields is an array of Field*, flds is just an array of Field objects.

Error 2: For pointers to classes, use -> instead of . (Or use (*ptrname).)
Last edited on
firedraco: thanks. OK I've changed the subject of Error 2 thus:

1
2
printf(fields[i]->fldPrompt);
fields[i]->startRow+strlen(fields[i]->fldPrompt));


For Error 1, I assume you mean that I should change the ctor signature to this:

 
Form(Field **flds, unsigned short numFlds, void **dta);


If I then instantiate Form thus:

 
Form  frm(**fld, numfields, **dat);


I get :

C:\TCPP\WORKAREA\FORMTEST\MYFRMTST.CPP error : 'void*' is not a pointer-to-object type|
||=== Build finished: 1 errors, 8 warnings ===|

so I'm still struggling a bit here!

My first question, do you HAVE to use a void*? All the data seems to be a char*, and if it's not, how are you going to figure out what type it is?

Also, I would suggest replacing your arrays with std::vectors or something similar, and your char* with std::strings since they are much easier to use and error check (unless you are stuck using those for a class or something)
No, I don't have to use void*, at least for this exercise. So I've changed that. I also had to change the initialization of char *dat[] to

 
char  *dat[] = { &firstName[13], &surName[26], &gender[2], &age[3] };


to avoid errors about not being able to convert firstname[13] to *char etc.

I'm left with one error:

C:\TCPP\WORKAREA\FORMTEST\MYFRMTST.CPP error: no matching function for call to 'Form::Form(Field&, const int&, char&)'|
C:\TCPP\WORKAREA\FORMTEST\myform.h|29|note: candidates are: Form::Form(Field**, short unsigned int, char**)|
C:\TCPP\WORKAREA\FORMTEST\myform.h|23|note: Form::Form(const Form&)|
||=== Build finished: 1 errors, 8 warnings ===|

stemming from:

 
Form  frm(**fld, numfields, **dat);


Regarding use of std::vectors and std::strings, I wanted to get the basic C->C++ conversion working first. Using vectors/strings is next on the list.
Topic archived. No new replies allowed.