tags:

views:

136

answers:

2

I have a program for distance vector routing as shown below (though, what the program is for is not important here). The problem is when I run this program using Turbo C++ compiler on Windows, it works perfectly fine.

But when I compile this using gcc on Fedora 9 (as you can see the 'r' I have used in the function build of the 'router' class) the compiler tells that it is used before the declaration, but in turbo c++ it's fine.

Any options to get through this error in gcc compiler, or any modifications to the function to perform the same task? Please, please, help me - in a very needy state - help will be remembered...

#include <conio.h>
#include <iostream.h>

#define MAX 10
int n;

class router {
    char adj_new[MAX],   adj_old[MAX];
    int  table_new[MAX], table_old[MAX];

  public:
    router(){
      for(int i=0;i<MAX;i++) table_old[i]=table_new[i]=99;
    }

    void copy(){
      for(int i=0;i<n;i++) {
        adj_old[i]  =adj_new[i];
        table_old[i]=table_new[i];
      }
    }

    int equal() {
      for(int i=0;i<n;i++)
        if(table_old[i]!=table_new[i]||adj_new[i]!=adj_old[i])return 0;
      return 1;
    }

    void input(int j) {
      cout<<"Enter 1 if the corresponding router is adjacent to router"
          <<(char)('A'+j)<<" else enter 99: "<<endl<<"             ";
      for(int i=0;i<n;i++) 
        if(i!=j) cout<<(char)('A'+i)<<" ";
      cout<<"\nEnter matrix:";
      for(i=0;i<n;i++) {
        if(i==j)
          table_new[i]=0;
        else
          cin>>table_new[i];
        adj_new[i]= (char)('A'+i);
      }
      cout<<endl;
    }

    void display(){
      cout<<"\nDestination Router: ";
      for(int i=0;i<n;i++) cout<<(char)('A'+i)<<" ";
      cout<<"\nOutgoing Line:      ";
      for(i=0;i<n;i++) cout<<adj_new[i]<<" ";
      cout<<"\nHop Count:          ";
      for(i=0;i<n;i++) cout<<table_new[i]<<" ";
    }

    void build(int j) {
      for(int i=0;i<n;i++)
        for(int k=0;(i!=j)&&(k<n);k++)
          if(table_old[i]!=99)
            if((table_new[i]+r[i].table_new[k])<table_new[k]) {
              table_new[k]=table_new[i]+r[i].table_new[k];
              adj_new[k]=(char)('A'+i);
            }
    }
} r[10];
+1  A: 

GCC is right, this is not standard-compliant C++ code (seeing as even a for loop variable "i" is not redeclared sometimes).

Declare the array r separately as a static member of the class:

// Header file (declaration):

class router {
    ...
private:
    static router r[10];
};

// CPP file (definition)
router router::r[10];

PS

However, I'd mention that the design of the class is questionable. A class should encapsulate some single responsibility and should not manipulate collections of itself, this functionality belongs to outside of the class.

Alex B
+2  A: 

Turbo C++ is a very old compiler for MS-DOS, although it was renewed in 2006.

  • Remove conio.h as you will unlikely have it on Fedora
  • scope of variable declared in for loop is now reduced to the loop you have to redeclare i as an int in every for(i=0 ... statement
  • define the void router::build(int j) method outside of the router class :

    class router {

     void router::build(int j)
    

    } r[10];

    void router::build(int j) { ...}

  • Compile with g++ -ansi (EDIT no required here)

philippe