Question

I have to make a program calculating on my new type of symbols and I have encountered a problem I cannot solve.

Code piece:

typedef struct{
  Symbol a1, a2, w_wcz, w_ok; 
  char oper, rown;     

  int Oblicz(){ 
  if(rown != '='){
    cerr << "Blad, brak symbolu rownosci!" << endl;
    return 0;}
  switch(oper){
  case '+':
    w_ok=a1+a2;

  case '-':
    w_ok=a1-a2

  case '*':
    w_ok=a1*a2;

  case '/':
    w_ok=a1/a2;       

  default:
    cerr << "Blad, nieznany operator!" << endl; 
    return 0;}

  }
}WyrAlg;

a,b,c,d,e are of enum Symbol type. +, -, * and / operators are overloaded for two symbols and are working fine, it's only getting problematic when I try to put it in method.

In main I have

WyrAlg Wyr;
[it is initialized here with values]
Wyr.Oblicz();

And what I'm getting is:

[tjakubo2@diablo:~/kpo/lab04]$ make
g++ -pedantic -Wall -o dzialok.out dzialaniamod.cpp
dzialaniamod.cpp: In member function 'int<anonymous struct>::Oblicz()':
dzialaniamod.cpp:32:8: error: invalid conversion from 'int' to 'Symbol' [-fpermissive]
     w_ok=a1+a2;
            ^     
dzialaniamod.cpp:40:12: error: invalid conversion from 'int' to 'Symbol' [-fpermissive]
     w_ok=a1-a2;
            ^
dzialaniamod.cpp:48:12: error: invalid conversion from 'int' to 'Symbol' [-fpermissive]
     w_ok=a1*a2;
            ^
dzialaniamod.cpp:56:12: error: invalid conversion from 'int' to 'Symbol' [-fpermissive]
     w_ok=a1/a2;
            ^
*** Error code 1
make: Fatal error: Command failed for target `dzialok.out'

And I can't figure what going on, need help on this. Thanks in advance!

EDIT: Including overloaded operator: (may be unclear but it works fine)

enum Symbol {e,a,b,c,d};
Symbol tab_dod[5][5]= {{e,a,b,c,d},{a,b,c,d,e},{b,c,d,e,a},{c,d,e,a,b},{d,e,a,b,c}};

Symbol operator + (Symbol x, Symbol y){

   return tab_dod[x][y];
}

CLARIFICATION EDIT 2: Function-version of that is working ok, Ill paste it here if you need it: The only difference is that w_ok is outside WyrAlg, insted it is only in function:

int ObliczWyr(WyrAlg Wyr){
  Symbol w_ok;
  if(Wyr.rown != '='){
    cerr << "Blad, brak symbolu rownosci!" << endl;
    return 0;}
  switch(Wyr.oper){
  case '+':
    w_ok=Wyr.a1+Wyr.a2;
   [stuff]
      return 2;}
  case '-':
    w_ok=Wyr.a1-Wyr.a2;
  [stuff]
      return 2;}
  case '*':
    w_ok=Wyr.a1*Wyr.a2;
   [stuff] 
      return 4;}
  case '/':
    w_ok=Wyr.a1/Wyr.a2;
    [stuff]
      return 4;}
  default:
    cerr << "Blad, nieznany operator!" << endl; // jesli nieznany operator
    return 0;}

}
Was it helpful?

Solution

This code is compiled successfully

#include <iostream>

enum Symbol {e,a,b,c,d};
Symbol tab_dod[5][5]= {{e,a,b,c,d},{a,b,c,d,e},{b,c,d,e,a},{c,d,e,a,b},{d,e,a,b,c}};

Symbol operator + (Symbol x, Symbol y)
{
    return tab_dod[x][y];
}

int main()
{
    typedef struct
    {
        Symbol a1, a2, w_wcz, w_ok; 
        char oper, rown;     

        int Oblicz()
        { 
            switch(oper)
            {
                case '+':
                    w_ok=a1+a2;
                    break;
                default:
                    std::cerr << "Blad, nieznany operator!" << std::endl; 
                    break;
            }

            return 0;
        }
    } WyrAlg;

    WyrAlg alg;
    alg.a1 = e;
    alg.a2 = a;
    alg.oper = '+';
    alg.Oblicz();
}

Maybe you declared the operator after the structure definition.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top