0

I'm working on a school project with C++, I'm using constructors. I will have a basic school system, which will consist on working with students name, student id, student grades (with will be the float array[]) and an optional substitution test.
I'm having problems when assigning the float array to the values I have declared on the main function.
This is the function.cpp file:

#include "aluno.h"
#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>

using namespace std;

Aluno::Aluno(){
    cout << "Objeto 1 inicializado!\n";
}

Aluno::Aluno(char* name, int rgm, float notas[2], float nota_sub){
    this->nome = name;
    this->rgm = rgm;
    this->notas = notas;
    this->nota_sub = nota_sub;
    cout << "Objeto 2 inicializado!\n";
}

Aluno::~Aluno(){
    cout << "Aluno destruido: " << nome;
    cout << "Objeto destruido!";
}

void Aluno::entrada(){
    cout << "Digite seu NOME: ";
    cin >> nome;
    cout << "Digite seu RGM: ";
    cin >> rgm;
    cout << "Digite sua nota do primeiro bimestre: ";
    cin >> notas[0];
    cout << "Digite sua nota do segundo bimestre: ";
    cin >> notas;
    cout << "Digite sua nota da substutiva: ";
    cin >> nota_sub;
}

When attempting to do this->notas = notas I get the following error:

expression must be a modifiable lvalueC/C++(137)

This is the .h file:

#ifndef ALUNO_H
#define ALUNO_H

class Aluno
{
    private:
        char* nome;
        int rgm;
        float notas[2];
        float nota_sub;
    public:
        Aluno();
        Aluno(char* name, int rgm, float notas[2], float nota_sub = 11);
        ~Aluno();
        void entrada();
        void imprime();
        bool aprovado();
};

#endif 

And the main file:

#include <iostream>
#include "aluno.h"

using namespace std;

int main()
{
    float notas[2] = {5.3, 6.2};

    Aluno aluno1("Nilton", 1904659, notas);

    return 0;
}
Nilton Schumacher F
  • 814
  • 3
  • 13
  • 43
  • 1
    Well, yeah. You can't copy-assign arrays. This is C++ 101. Use `std::copy`. – paddy Mar 30 '21 at 01:04
  • 1
    Also note that `float notas[2]` in `Aluno::Aluno` likely doesn't mean what you think it means. See [Passing Arrays to Function in C++](https://stackoverflow.com/questions/14309136/passing-arrays-to-function-in-c) – Brian61354270 Mar 30 '21 at 01:06
  • 1
    You can't copy arrays in C/C++ using just `operator=`. You can try `this->notas[0] = notas[0]; this->notas[1] = notas[1];`. Perhaps a shorter notation exists. You would save yourself (and us) the effort if you used `std::array` instead of `float[2]` - then copying would be easy. – zkoza Mar 30 '21 at 01:08
  • 1
    paddy is correct about the syntax error. You actually have *several* options. One is to use [std::copy](https://www.cplusplus.com/reference/algorithm/copy/). Another option is to declare your array without a size: `float notas[]`. The best option is to use an array container [std::array](https://en.cppreference.com/w/cpp/container/array), instead of a "raw" array. – paulsm4 Mar 30 '21 at 01:08

0 Answers0