0

I am so confused right now...

Here is the code that I will be talking about:

main.cpp:

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

int main() {

    CustomVector<int, 15> hello{};

    std::cout << hello.size() << '\n';

    return 0;
}

CustomVector.h:

#pragma once

template<typename T, int S>
class CustomVector {

private:
    T arr[S];
    int size;

public:
    CustomVector() : arr{}, size{ S } {}

    // Methods
    int size() const {
        return size;
    }
};

As you can see I am trying to use the size() method that I have in my class definition, which there shouldn't be a problem with, right..? But when I try to use it in main.cpp and try to print it out to the console, it is giving me the compiler error which my question title has. Why could this be, this has never happened to me before. I would think there is some sort of "redefinition" somehow, but how could that be, there couldn't be a redefinition if it is in my own user-defined class?

Krapnix
  • 257
  • 2
  • 8
  • 2
    Having a member variable and a member function with the same name is likely to cause trouble. – Paul Sanders Jan 25 '21 at 23:13
  • @PaulSanders so do you think it is my attribute that is the root of this problem? I will try that changing it now, hopefully it fixes it, thank you! – Krapnix Jan 25 '21 at 23:15
  • Common practise is to use `m_` as a prefix for member variables, so `m_size` here. – Paul Sanders Jan 25 '21 at 23:15
  • @PaulSanders That has fixed it but now I am kind of wondering, why did that fix it, why did the compiler get confused in the first place, if I'm trying to access a member variable I would never put `()`after it because that is invalid, so wouldn't it be clear that I was trying to access a member function? – Krapnix Jan 25 '21 at 23:16
  • Did you get any other compiler errors? I would expect you to. – Paul Sanders Jan 25 '21 at 23:17
  • @PaulSanders I got a few now that I have retested the old code, one of the errors that I feel like are more important is this one: ` 'CustomVector::size': redefinition; previous definition was 'data member' `, but again is there a rule that I can't define a function with the same name as a data member? I am not very experienced in C++ yet and maybe that is why it sounds stupid. – Krapnix Jan 25 '21 at 23:22
  • 2
    _is there a rule that I can't define a function with the same name as a data member_ Yes there is. Sounds like you could [use a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Paul Sanders Jan 25 '21 at 23:23
  • @PaulSanders Ohhhh, thank you so much, new thing learnt today! – Krapnix Jan 25 '21 at 23:24
  • 1
    @Krapnix "*if I'm trying to access a member variable I would never put `()` after it because that is invalid*" - not if the member has an `operator()` implemented, for instance. – Remy Lebeau Jan 25 '21 at 23:59
  • In C++, if there is one error, you can get a lot of errors because of that one. So given the redefinition, the compiler probably ignore the second definition and then only see the first one which is private. If you would reverse the declaration order, you would get different errors. Often, one should look more carefully errors that come first in the compiler output. – Phil1970 Jan 26 '21 at 02:08

0 Answers0