3

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.

13
  • 4
    Use std::array or std::vector. This is not C. Commented Apr 1, 2018 at 3:12
  • The specification was provided by the lecturer. Can explain more what mean by this is not C? Commented Apr 1, 2018 at 3:14
  • 3
    Possible duplicate of C++ Error: Incompatible types in assignment of ‘char*’ to ‘char [2] Commented Apr 1, 2018 at 3:14
  • 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. Commented Apr 1, 2018 at 3:21
  • 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. Commented Apr 1, 2018 at 4:18

3 Answers 3

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 
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.