character arrays

so i keep getting the errors

1.syntax error: identifier 'Technichian'
2.'='; cannot convert from char[] to char[20]
3.'{' missing old function header (old style format list?)

i'm not sure how to fix them now

#include<iostream>
#include<string.h>
using namespace std;
class RepairJob
{
void friend displayRepairJob(RepairJob rj, Technician tech);
private:
int jobNum;
int mo;
int day;
int yr;
double amount;
int techNum;
public:
RepairJob(int job, int m, int d, int y, double amt, int tech);
};
RepairJob::RepairJob(int job, int m, int d, int y, double amt, int tech)
{
jobNum = job;
mo = m;
day = d;
yr = y;
amount = amt;
jobNum = tech;
}
class Technician
{
friend void displayRepairJob(RepairJob rj, Technician tech);
private:
int techId;
char name[20];
public:
Technician(int id, char nm[]);
};
Technician::Technician(int id, char nm[])
{
techId = id;
name = nm;
}

{void displayRepairJob(RepairJob rj, Technician tech)
cout << "RepairJob " << rj.jobNum << " on " << rj.mo << "/" << rj.day << "/" << rj.yr <<
" for $" << rj.amount << " repaired by #" << tech.techId << " " << tech.name << endl;
}
void main()
{
RepairJob job(1386, 9, 2, 2003, 228.95, 345);
Technician tech(345, "Dunne"),
displayRepairJob();
system("pause");
}
class repairjob refers to class technician but has not seen it yet. Because these classes use each other, you need what is called a forward declaration. I haven't done that in a while so you may need a google it (I am short on time) but I think that just means put
class Technician;
at the top of the file.

I have low patience for [] notation in prototypes. * notation solves most of those types of problems (strings and vectors solve them much better).

void foo (char *cp)
{
cout << cp << endl;
}
..
char x[100];
sprintf(x,"hello and stuff");
foo(x); //works

and #3 is probably that strange block of code above main. I think the { is before the function name, move it past that like all your others.

void main is not legal in standard c++. main is type int.


Last edited on
First, again, please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/

Second, the error messages tell also which line the error is on. Details are important.
6:44: error: 'Technician' has not been declared

 In constructor 'Technician::Technician(int, char*)':
38:6: error: incompatible types in assignment of 'char*' to 'char [20]'

 At global scope:
41:1: error: expected unqualified-id before '{' token
45:11: error: '::main' must return 'int'

 In function 'int main()':
48:29: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]


Ok, first error is on line 6:
6:44: error: 'Technician' has not been declared
1
2
3
4
5
6
#include<iostream>
#include<string.h>
using namespace std;
class RepairJob
{
  void friend displayRepairJob(RepairJob rj, Technician tech);

Do you see Technician declared anywhere within the first 6 lines, before the point of error? You cannot use something before you know what it is.

 In constructor 'Technician::Technician(int, char*)':
38:6: error: incompatible types in assignment of 'char*' to 'char [20]'
1
2
3
4
5
Technician::Technician(int id, char nm[])
{
  techId = id;
  name = nm;
}

The name is an array. The nm is a pointer. You cannot assign a pointer to array. You cannot assign an array to array either.

You could copy elements from one array to an another.
You could use std::string in place of arrays, for it supports some assignment types.

 At global scope:
41:1: error: expected unqualified-id before '{' token
{void displayRepairJob(RepairJob rj, Technician tech)
Are you sure that the { is where it should?

Which of these is correct and why?
1
2
3
4
5
6
7
8
int foo(int x)
{
  return 2*x;
}

{int foo(int x)
  return 2*x;
}


45:11: error: '::main' must return 'int'
void main()
The main() must return int.

48:29: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

Not a show-stopper.
The "Dunne" is a string literal constant. You give its address that takes a non-const pointer. The function could attempt to modify memory via that pointer. The "Dunne" is in memory that is not permitted modify.
Topic archived. No new replies allowed.