weiboo pan
weiboo pan

Reputation: 43

C++ Not find members

Error message: "std::__cxx11::basic_string<char, std::char_traits, std::allocator>" No members "isSubsequence".

#include<iostream>
#include<string>
#include<unordered_map>
#include<vector>
using namespace std;

class Solution {
public:
    bool isSubsequence(string s, string t) {
        if(s.size()<t.size()) return false;
        unordered_map<char,int> window,need;
        for(char c:s) need[c]++;
        int left=0,right=0;
        int sum=0;
        while(right<t.size()){
            char c = t[right];
            right++;
            if(need.count(c)){
                window[c]++;
                if(window[c]==need[c]){
                    sum++;
                }
            }
            if(sum==need.size()){
                return true;
            }
        }
        return false;
    }
};

int main(){
    
    string s = "abc";
    string t = "ahbgdc";
    Solution s;
    s.isSubsequence(s,t);    
    return 0;
}

Why can't object s call class member functions?

Upvotes: 1

Views: 131

Answers (3)

Rohan Bari
Rohan Bari

Reputation: 7726

There's a naming conflict here:

string s = "abc";
//     ^
string t = "ahbgdc";
Solution s;
s.isSubsequence(s,t);
^               ^

Probably you wanted to do:

string str = "abc";
string t = "ahbgdc";
Solution s;
s.isSubsequence(str, t);

Upvotes: 0

Jacob Kaniuk
Jacob Kaniuk

Reputation: 175

You have conflicting variable names. You have a string object named 's' and a Solution object named 's'. You mean to call the isSubsequence method on the Solution object, but it's being run on the string object instead. Simply change the variable names and you should be good.

Upvotes: 2

ViggyPiggy
ViggyPiggy

Reputation: 137

You have the same name for both the string and the solution object this is likely causing the name conflicts

Upvotes: 3

Related Questions