0

So i have a pretty straight foward homework that consist in creating a student class that has a name and 3 grades as attributes and a method to caluculate the final grade and append the name as well as the final grade to 2 vectors respectively, the problem comes up when i try to append the name to the vector as its appended as an empty string, but the debugger shows the instance of that student class (the "Alumno" class) has actually a name.

i'll leave you the code below,

class libroDeClases {
public:
    vector<string> nombres;
    vector<float> notasDef;
};

class Alumno {
private:
    string nombre;
    float n1, n2, n3;
    float notaDef;

public:
    Alumno(string nombre, float x, float y, float z) {
        nombre = nombre;
        n1 = x;
        n2 = y;
        n3 = z;    }
    void calcularNota(libroDeClases L) {
        float nd = (n1 + n2 + n3) / 3;
        notaDef = nd;
        L.notasDef.push_back(nd);
        L.nombres.push_back(nombre);
    } 

int main() {
    libroDeClases Libro;
    Alumno a1("Oscar", 4.0, 4.7, 5.5);
    a1.calcularNota(Libro);

thank you for your help!

Edit: i added the "Libro" class in order to make the code compile, i forgot to provide it sorry about that.

Tomás
  • 17
  • 4
  • 1
    Name the string in your class something else or use `this->` – Taekahn Mar 27 '22 at 02:46
  • 2
    Always a bad idea to give to variables reachable from the same scope the same name... – user17732522 Mar 27 '22 at 02:46
  • 2
    Clang: [*warning: explicitly assigning value of variable of type 'std::string' (aka 'basic_string') to itself*](https://gcc.godbolt.org/z/5oKadoxhs) – chris Mar 27 '22 at 02:46
  • Your code does not compile, you didn't provide an argument to `a1.calcularNota();`. Additionally, `void calcularNota(libroDeClases L)` should take its argument by reference, otherwise the function will just be modifying a copy of the argument. – François Andrieux Mar 27 '22 at 02:48
  • *"the debugger shows the instance of that student class (the "Alumno" class) has actually a name."* -- I doubt this, since nowhere in your code do you give a value to the member `nombre`. (Yes, I see the self-assignment `nombre = nombre;`.) Did you check if the student object has a name while in the `calcularNota` function (where there should be no confusion as to what the identifier `nombre` refers)? – JaMiT Mar 27 '22 at 05:33

2 Answers2

0

As the user Taekahn said in a comment, i used This -> and it now appends it perfectly.

Thank you.

Tomás
  • 17
  • 4
  • It might be better just to use a different name for the parameter. In a construcor I like to prepend an `i`. Thus `inombre`. – Spencer Mar 27 '22 at 13:07
  • Even better would be a [member initializer list](https://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor), in which case using the same name for the parameter is not a problem, as long as the constructor body is empty. – JaMiT Mar 27 '22 at 17:28
-1

If you pass the object by reference to calcularNota then the string gets printed successfully. If you just pass by value, a copy of the object gets made but doesn't change the value of the original object: https://godbolt.org/z/fGzWvzW1b

BobbyDude
  • 59
  • 3
  • *"then the string gets printed successfully"* ?? Where in the question is there code to print anything? You seem to have latched on to a different problem than the cause of the current symptom. – JaMiT Mar 27 '22 at 08:23
  • I was trying to show how the data gets appended once you pass the object by reference :P – BobbyDude Mar 27 '22 at 20:01
  • The OP already successfully appended the data (according to the information in the question); showing how to do that does not address the question. You are trying to answer the question "why is nothing appended", but the actual question is "why is the empty string appended". Different issue. – JaMiT Mar 27 '22 at 20:12