Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

c++ - 'CustomVector<int,15>::size': cannot access private member declared in class 'CustomVector<int,15>'

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() << '
';

    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?

question from:https://stackoverflow.com/questions/65893684/customvectorint-15size-cannot-access-private-member-declared-in-class-cu

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...