Structures

I am trying to run a debug on this program but i just can't solve the errors.This is basic program of structure using with pointers and functions!

bx1.cpp

double dmodul (COMPLEX *z)
/*calculeaza si returneaza modulul numarului complex z*/
{
return sqrt(z->x*z->x+z->y*z->y);
}


bx2.cpp

double darg (COMPLEX *z)
{
double a;

if(z->x==0 && z->y==0)return 0.0;
if(z->y==0)
if(z->x>0)return 0.0;
else /*y=0 si x<0*/
return PI;
if(z->x==0)
if(z->y>0)return PI/2;
else /*x=0 si y<0*/
return (3*PI)/2;

/*x!=0 si y!=0*/
a=atan(z->y/z->x);
if(z->x <0)/*x>0 si y<0 */
return a+PI;
else /*x>0*/
if(z->y < 0) /*x>0 si y<0*/
return 2*PI + a;

/*x>0 si y>0*/
return a;
}


bx3.cpp


#include<stdio.h>
#include<math.h>


#define PI 3.14159265358979

typedef struct{
double x;
double y;
} COMPLEX;

#include "bx1.cpp"
#include "bx2.cpp"

main()/*citeste nr compplexe si le afiseaza impreuna cu modulul si argumentul corespunzator*/
{
COMPLEX numar;
while(scanf("%lf %lf",&numar.x,&numar.y)==2)
{
printf("a+ib=%g +i*(%g)/n",numar.x,numar.y);
printf("modul=%g\targ=%g\n",dmodul(&numar),darg(&numar));
}
}


The errors i receive:

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

error C2065: 'COMPLEX' : undeclared identifier
error C2065: 'z' : undeclared identifier
error C2448: 'darg' : function-style initializer appears to be a function definition
error C2065: 'COMPLEX' : undeclared identifier
error C2065: 'z' : undeclared identifier
error C2448: 'dmodul' : function-style initializer appears to be a function definition

PS:I am using Microsoft Visual c++ 2010
Last edited on
COMPLEX is not a type you haven't specify the type name:

typedef struct{


it should be

typedef struct some_name
Last edited on
i tried this!no effect!i reveive the same errors!
closed account (DSLq5Di1)
COMPLEX is fine as is, but it is not defined in bx1.cpp or bx2.cpp which is causing the error.

You may be thinking because you've defined COMPLEX before including either of those files that it should be visible to the functions in those files..

Each file is compiled as a seperate translation unit, bx1.cpp & bx2.cpp do not know that they are being included into another source file and do not know COMPLEX is defined before them in bx3.cpp.

You could copy your COMPLEX type into the other files, but it would be much better if you created a seperate header to include in each file.

bx.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef BX__H
#define BX__H

#include <cmath>

const double PI(3.14159265358979);

typedef struct {
    double x;
    double y;
} COMPLEX;

double dmodul(COMPLEX*);
double darg(COMPLEX*);

#endif 

bx1.cpp
1
2
3
4
5
6
7
#include "bx.h"

double dmodul (COMPLEX *z)
/*calculeaza si returneaza modulul numarului complex z*/
{
    return sqrt(z->x*z->x+z->y*z->y);
}

bx2.cpp
1
2
3
4
5
6
#include "bx.h"

double darg (COMPLEX *z)
{
    ...
}

bx3.cpp
1
2
3
4
5
6
7
8
9
10
#include <cstdio>
#include "bx.h"

int main()/*citeste nr compplexe si le afiseaza impreuna cu modulul si argumentul corespunzator*/
{
    COMPLEX numar;
    ...

    return 0;
}
Last edited on
this solved the problem.and i begin to understand what a header is used for.tnx sloppy9 for your time and your COMPLEX explication;))
Topic archived. No new replies allowed.