I keep on getting "error: invalid conversion from 'const char*' to 'char*' [-fpermissive]"

Every time I run it, it would give me "Prob1.cpp:6:9: error: invalid conversion from 'const char*' to 'char*' [-fpermissive] set = rndseq;" can someone please help me fix this?

The purpose of the program: is to loop a.randFromSet(); 100,000 times and give back the frequency of each time a number in rndseq[] has been called.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

#include "Prob1.h"

int main(int argc, char** argv) {
    char n=5;
    char rndseq[]={16,34,57,79,144};
    int ntimes=100000;
    
    Prob1Random a(n,rndseq);
    
    for(int i=1;i<=ntimes;i++){
	 a.randFromSet();
    }

    int *x=a.getFreq();
    char *y=a.getSet();

    for(int i=0;i<n;i++){
	 cout<<int(y[i])<<" occured "<<x[i]<<" times"<<endl;
    }

    cout<<"The total number of random numbers is "<<a.getNumRand()<<endl;
    return 0;
}



Prob1.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

#ifndef PROB1_H
#define	PROB1_H



class Prob1Random
{
	private:
		char *set;      //The set of numbers to draw random numbers from
		char  nset;     //The number of variables in the sequence
		int  *freq;     //Frequency of all the random numbers returned
		int   numRand;  //The total number of times the random number function is called
	public:
		Prob1Random(const char,const char *);     //Constructor
		~Prob1Random(void);                       //Destructor
		char randFromSet(void);                   //Returns a random number from the set
		int *getFreq(void) const{ return freq; };                 //Returns the frequency histogram
		char *getSet(void) const{ return set; };                 //Returns the set used
		int getNumRand(void) const{ return numRand; };               //Gets the number of times randFromSet has
		                                          //been called
};




Prob1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "Prob1.h"

//The constructor
Prob1Random::Prob1Random(const char arrSize, const char *rndseq){
    nset = arrSize;
    set = rndseq;

    //set all values in array freq[] to 0
    for(int count = 0; count < 5; count++){
        freq[count] = 0;
    }

    numRand = 0;

};

//Returns random number and set freq[] and int numRand
char Prob1Random::randFromSet(void){
    int arrCount;

        //get the system time
        unsigned seed = time (0);

        //seed the random number generator
        srand(seed);

        arrCount = rand()%5;
        
        numRand += 1;

        //setting freq[]
        if(arrCount == 0){
            freq[0] += 1;
        }
        else if(arrCount == 1){
            freq[1] += 1;
        }
        else if(arrCount == 2){
            freq[2] += 1;
        }
        else if(arrCount == 3){
            freq[3] += 1;
        }
        
        else if(arrCount == 4){
            freq[4] += 1;
        }

        return set[arrCount];
};


//The Destructor
Prob1Random::~Prob1Random(void){
    delete set;
    delete freq;
};
Last edited on
"Error: a value of type "const char *" cannot be assigned to an entity of type "char *""

The whole point of "const" is that the value never gets changed. When you do "set = rndseq" you are assigning something that can't be changed (const char *) to something that can (char *). You are also copying the address (since both of these are pointers), and that will guarantee that "seq" can change whatever rndseq is looking at it.

The problem is on "Prob1.cpp" line 6.
Last edited on
Another problem is
1
2
    delete set;
    delete freq;
You delete what you never new'd.
Okay, when I remove the delete set; and delete freq; and changed the "const char*" to char * it gives me "RUN FAILED (exit value 1, total time: 569ms)".
1
2
3
4
5
       //get the system time
        unsigned seed = time (0);

        //seed the random number generator
        srand(seed);
You should seed only once in the main function.

Though the error seems like some sort of endless loop. Maybe try throwing a couple output statements to see where exactly it hangs at.
I threw a couple output statements around and it error happens to be every single one of my for- loops. I removed them one by one up until the last one and it ran without any errors afterwards. Let me try and fix it and I will report back.
Last edited on
The error happens to be everything associated with freq[].
I just noticed but freq is uninitialized look at your constructor
Topic archived. No new replies allowed.