vendredi 14 août 2020

Why am I getting this error-:expected primary-expression before 'bool'?

#include<iostream>
#include<string>


using namespace std;
class LFSR
{
private:

    bool lfsr[];
    private: int tap;
    private: int t;
    private: string s;

public: 
    LFSR(string seed, int tap)
    {
        s = seed;
        t = tap;


        lfsr = bool[s.length()];

        t = s.length() - 1 - t;

        for (int i = 0; i < seed.length(); i++)
        {

            if (seed.charAt(i) == 48)
            {

                lfsr[i] = false;
            }
            else
            {

                lfsr[i] = true;
            }
        }
    }
    int step()
    {

        boolean newBit = lfsr[0] ^ lfsr[tap];
        for (int i = 0; i < lfsr.length - 1; i++)
        {

            lfsr[i] = lfsr[i + 1];
        }
        lfsr(lfsr.length - 1) = newBit;
        return newBit == false ? 0 : 1;
    }
    int generate(int k)
    {

        int temp = 0;
        for (int i = 0; i < k; i++)
        {

            temp = 2 * temp;
            temp = temp + step();

        }
        return temp;

    }
};
int main()
{

    LFSR lfsr("01101000010", 8);
    for (int i = 0; i < 10; i++)
    {

        int bit = lfsr.step();
        cout << lfsr << "+" << " " << bit;
    }
    LFSR lfsr("0110100010", 8);
    cout << "Testing generate()";

    for (int i = 0; i < 10; i++)
    {

        int r = lfsr.generate(5);
        cout << lfsr.to_string() << " " << r;
    }
    return 0;
}

At this line :

      lfsr=bool[s.length()];

I am getting the above stated error.

What am I trying to do?

I am trying to create a LFSR of size 10(seed length or number of registers).

My questions?

Why am I getting this errror?

How can I fix this error?

The code in java that I am trying to do in C++ I will give you the code, let's see.

public class LFSR {
 private boolean[] lfsr;
 private int tap;
/**
 * @param args the command line arguments
 */
public static void main(String[] args)
 {
 LFSR lfsr = new LFSR("01101000010", 8);
System.out.println("Testing step()");
 for (int i = 0; i < 10; i++)
 {
 int bit = lfsr.step();
System.out.println(lfsr + " " + bit);
}
 lfsr = new LFSR("01101000010",8);
System.out.println("\nTesting generate()");
 for (int i = 0; i < 10; i++)
 {
 int r = lfsr.generate(5);
System.out.println(lfsr + " " + r);
}
 }
 public LFSR(String seed, int tap)
 {
 lfsr = new boolean[seed.length()];
 this.tap = (seed.length()-1)-tap;
 for(int i = 0; i < seed.length(); i++)
 {
 if(seed.charAt(i) == 48)
 {
 lfsr[i] = false;
}
 else
{
 lfsr[i] = true;
}
 }
 }

 public int step()
 {
 boolean newBit = lfsr[0] ^ lfsr[tap];
 for(int i = 0; i < lfsr.length-1; i++)
 {
 lfsr[i] = lfsr[i+1];
}
 lfsr[lfsr.length-1] = newBit;
 return newBit == false ? 0 : 1;
}
 public int generate(int k)
 {
 int temp = 0;
 for(int i = 0; i < k; i++)
 {
 temp *= 2;
temp += step();
}
 return temp;
}
 public String toString()
 {
 String representation = "";
 for(int i = 0; i < lfsr.length; i++)
 {
 representation += lfsr[i] == false ? 0 : 1;
}
 return representation;
}
}

Aucun commentaire:

Enregistrer un commentaire