5 Replies - 1840 Views - Last Post: 01 December 2013 - 06:35 PM Rate Topic: -----

#1 Psycoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-December 13

Error in C++: "expected primary-expression before '[' toke

Posted 01 December 2013 - 03:19 PM

My C++ program is giving me an error, it says the next: "expected primary-expression before '[' token" in this section of the code:

void altasv (){
     
typedef empleados empleosAltas[25];
	
    clrscr();
	cuadro();
   gotoxy(20,3);
   cout<<"Datos del Vendedor";
   gotoxy(20,7);
   cout<<"Captura el  Nombre: ";
   gets(nombrev[i]);
   gotoxy(20,8);
   cout<<"Captura el Sexo: ";
   cin>>sexov[i];
   gotoxy(20,9);
   cout<<"Captura la Edad: ";
   cin>>edadv[i];
   gotoxy(20,10);
   cout<<"Captura el Area: ";
   gets(area[i]);
   gotoxy(20,11);
   cout<<"Captura el Sueldo: ";
   cin>>sueldo[i];
   gotoxy(20,13);cout<<"Captura el codigo de empleado: ";
   cin>>cod_empleado;
   
   claves[i] = cod_empleado;
   
   gotoxy(20,16);cout<<"Desea capturar otro? S/N";
       cin>>r;
       
       i++;
       b++;
       
       if(r=='S' || r=='s'){
          clrscr();
          cuadro();
          altasv();         
       } else if(r=='N' || r=='n'){
          clrscr();
          cuadro();
          empleosAltas[b].menu_empleados();       
       }
}//void altasv



In this, the 'empleados empleosAltas' is an structure that I declared inline so I can call the functions from it.

The line that is failing is:

 'empleosAltas[b].menu_empleados();' 


That line throws the error: "expected primary-expression before '[' token"

'menu_empleados()' is a function inside an structure.

The question is: why is it giving me those errors? I can't seem to find anything related in Google, for what I've seen is that maybe is the declaration of the structure, but I'm not 100% sure though.

Is This A Good Question/Topic? 0
  • +

Replies To: Error in C++: "expected primary-expression before '[' toke

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Error in C++: "expected primary-expression before '[' toke

Posted 01 December 2013 - 03:57 PM

Hi, typedef creates a type and not a variable. Were you wanting to create a variable?
Was This Post Helpful? 0
  • +
  • -

#3 Psycoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-December 13

Re: Error in C++: "expected primary-expression before '[' toke

Posted 01 December 2013 - 04:29 PM

View Post#define, on 01 December 2013 - 03:57 PM, said:

Hi, typedef creates a type and not a variable. Were you wanting to create a variable?


Hi! No, I want to create an object of the structure so I can use the functions that are inside it (the structure), I don't know if this is even possible for I don't have too much experience with C++.
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Error in C++: "expected primary-expression before '[' toke

Posted 01 December 2013 - 04:43 PM

Hi, perhaps instead of :

03	typedef empleados empleosAltas[25];



declare an array of type empleados

03	empleados empleosAltas[25];



Then this should be ok.

  empleosAltas[0].menu_empleados();  



To be sure we would need to see more code and how empleados was declared.

Have you initialized i and b?

32	       i++;
33	       b++;




C++ Language Tutorial
.

This post has been edited by #define: 01 December 2013 - 04:45 PM

Was This Post Helpful? 0
  • +
  • -

#5 Psycoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-December 13

Re: Error in C++: "expected primary-expression before '[' toke

Posted 01 December 2013 - 04:52 PM

View Post#define, on 01 December 2013 - 04:43 PM, said:

Hi, perhaps instead of :

03	typedef empleados empleosAltas[25];



declare an array of type empleados

03	empleados empleosAltas[25];



To be sure we would need to see more code and how empleados was declared.

Have you initialized i and b?

32	       i++;
33	       b++;




C++ Language Tutorial


Hi! I declared the struct empleados as it follows:

 struct empleados{
   void altas_empleados(){
       // Actions of the function
   }

   void bajas_empleados(){
       // Actions of the function 
   }

   void cambios_empleados(){
       // Actions of the function
   }
}empleados_menu[25]; // <-- This array from here it's completely ok in that place, but it's kind of pointless having it there, isn't it? 


I have already done the struct array but it yet throws me an error saying that "elements of array 'empleadosAltas[25] have incomplete type".

Just right after the structure empleados I have the structure where I want to use the object from the empleados structure.
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Error in C++: "expected primary-expression before '[' toke

Posted 01 December 2013 - 06:35 PM

Hi, there are several ways of doing this.

#include <iostream>
#include <string>

using namespace std;

#define MAXEMPLEADOS  10

struct Empleados
{
  string nombrev;
  int    edadv;

  void output();
  void input();
};


void Empleados::output()
{
  cout << nombrev << ", " << edadv << endl;
}

void Empleados::input()
{
  cout << "Captura el Nombre: ";
  getline(cin, nombrev);

  cout << "Captura la Edad: ";
  cin >> edadv;

  cin.ignore();
}

// pass array to function
void display(Empleados emp[], int size)
{
  for(int i=0; i<size; i++)  {
    emp[i].output();
  }
}

int main()
{
  Empleados empleados[MAXEMPLEADOS];
  int count = 3;

  for(int i=0; i<count; i++)  {
    empleados[i].input();
  }

  for(int i=0; i<count; i++)  {
    empleados[i].output();
  }

  cout << "Using display function" << endl;
  display(empleados, count);

  return 0;
}


.

This post has been edited by #define: 01 December 2013 - 06:37 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1