2

I got this error while coding a simple function. This is my function specification.

string studentName;
string courseTaken[3];
void setStudent(string, string[]); 

void Student::setStudent(string n, string a[])
{
   studentName= n;
   courseTaken = a;
}

This is the error I have gotten:

incompatible types in assignment of string* to string [3] on this line courseTaken = a;

In my code, I never declared any pointer or char.

I don't quite understand what is going wrong here.

Fabio_MO
  • 713
  • 1
  • 13
  • 26
Einn Hann
  • 651
  • 2
  • 8
  • 17
  • 4
    Use `std::array` or `std::vector`. This is not C. – user202729 Apr 01 '18 at 03:12
  • The specification was provided by the lecturer. Can explain more what mean by this is not C? – Einn Hann Apr 01 '18 at 03:14
  • 3
    Possible duplicate of [C++ Error: Incompatible types in assignment of ‘char\*’ to ‘char \[2\]](https://stackoverflow.com/questions/15914220/c-error-incompatible-types-in-assignment-of-char-to-char-2) – user202729 Apr 01 '18 at 03:14
  • Please post enough of the class to make a complete compilable example that exhibits the error. – Galik Apr 01 '18 at 03:17
  • So meaning, in C++ for array I can't just assign the array in this way. I need to do copy -> std::copy(std::begin(foo), std::end(foo), std::begin(bar)); – Einn Hann Apr 01 '18 at 03:19
  • 2
    C arrays don't work like this. When you pass it to a function, you're just passing a pointer to the first element around. The function declaration is the exact same as `void Student::setStudent(string n, string *a)`. By "this is not C", @user202729 means that you should use C++ containers instead of C arrays, which has some unintuitive things about it especially when you try to pass it to a function. He/she is likely referring to the common bad practice of teaching C stuff before C++ stuff. – eesiraed Apr 01 '18 at 03:21
  • Frankly, in C++ your function would be better off being a template and taking an iterator range as arguments. That's the way most of the standard library works, and its like that for a reason. If you're going ot add an argument (and you will, because right now the function has no clue how many elements are in the sequence `a[]`, you may as well think about doing it right. – WhozCraig Apr 01 '18 at 03:26
  • And `std::copy(std::begin(foo), std::end(foo), std::begin(bar));` won't work, because when arrays are passed into functions, no information on the size of the array is passed. C arrays also do not have `begin()` and `end()` functions, so you actually need to do pointer arithmetic. In the end, just use C++ containers. – eesiraed Apr 01 '18 at 03:27
  • Paraphrasing Yoda: C++. Or C++ not. There is no try. You don't want to become known as a C+ coder, that strange breed who never quite made the transition from C to C++ :-) – paxdiablo Apr 01 '18 at 03:38
  • Because the specification was given by lecturer. Fei Xiang, thanks for your explanation. I understand already. – Einn Hann Apr 01 '18 at 03:38
  • Maybe you could figure out a clever way to get your instructor to watch this video: https://youtu.be/YnWhqhNdYyk – Jive Dadson Apr 01 '18 at 04:13
  • What is `string`? `std::string`? – Jive Dadson Apr 01 '18 at 04:14
  • 1
    What can you change, and what does the instructor insist must not be changed? If you can't change anything, you can't fix anything. – Jive Dadson Apr 01 '18 at 04:18

3 Answers3

1

You can not assign array of strings string a[] to array courseTaken using = operator. The expression string a[] is equivalent to std::string*. That is why you get the compiler error.

This may be what you wanted:

#include <iostream>
using namespace std;

class Student
{
    public:
        string studentName;
        string courseTaken[3];
        void setStudent(string n, string a[]); 
};

void setStudent(string n, string a[]); 

void Student::setStudent(string n, string a[])
{
   studentName = n;
   for(int i=0; i < sizeof(courseTaken)/sizeof(courseTaken[0]); i++)
    courseTaken[i] = a[i];
}

int main()
{
    Student student;

    string courses[3] = {"Cobol","C++","Fortran"};
    student.setStudent("Eva", courses);

    for (int i = 0; i < 3; i++){
        cout << student.courseTaken[i] << endl;
    }

    return 0;
}

Output:

Cobol                                                                                                                                        
C++                                                                                                                                          
Fortran 
sg7
  • 6,108
  • 2
  • 32
  • 40
1

It seems you don't understand array decay mechanism of C-format arrays. For many context, an array name will be explained as a pointer to the first element of the array. And this pointer is a prvalue which just like this pointer, you can NOT assign to it. the "modern Cpp way"(C++11) is to use std::array, which overloaded the =operator and stores the size of the array so that it won't be decayed while passing to a function. The second way is to pass the reference, with a template you can ensure the array's size, then use std::memcpy. And you can add a parameter stores the array's size, and then you can use memcpy too. I hope you use the first way, don't forget -std=c++11

con ko
  • 1,368
  • 5
  • 18
0

This is because you are passing an array to a variable that's why this error occurs. To solve this problem you may use pointer in argument to solve this problem. Change your function to this.

void Student::setStudent(string n, string* a)
{
   studentName= n;
   courseTaken = a;
}