R. S
R. S

Reputation: 23

C++ I keep getting "was not declared in this scope error"

So, I have a header file and a class file. But when I compile puzzle.cpp, I keep getting get_solution was not declared in this scope. I don't get why that error occurs since its inside the same class so I can call any function inside the same class. Can anyone please help me on this? Thanks!

puzzle.h

#ifndef PUZZLE_H
#define PUZZLE_H

#include<iostream>
#include <string>
#include <vector>

class puzzle{
private:
    std::string _solution;
    std::vector<bool> _guesses;
public:
    puzzle(std::string solution);
    std::string get_solution(){return _solution;}
    bool guess(char c);
    bool solve(std::string proposed_solution);
    std::string to_string();
};

#endif

puzzle.cpp

#include <iostream>
#include "puzzle.h"
#include <string>
#include <vector>

using namespace std;

puzzle::puzzle(std::string solution) {
    _solution = solution;
   for(int i = 0; i < 256; i++)
      _guesses.push_back(false);
}

bool puzzle::guess(char c){
    int num = c;
    if(c<='z' || c>='a')
        if(_guesses.at(c) == false){
          _guesses.at(c) == true;
           return true;
       }
    return false;
}

bool solve(string proposed_solution){
    string test = get_solution();
    if(proposed_solution.compare(test) == 0)
       return true;
    return false;
}

string to_string(){
   int len = get_solution().length();
   return "";
}

Upvotes: 2

Views: 1124

Answers (3)

Mureinik
Mureinik

Reputation: 311188

solve and to_string are supposed to be methods, so you need to prefix them with the class' name followed by two colons (i.e., puzzle::):

bool puzzle::solve(string proposed_solution){
    // Code ...
}

string puzzle::to_string(){
    // Code ...
}

Upvotes: 2

scohe001
scohe001

Reputation: 15446

It looks like you've forgotten to make solve and to_string member functions:

Change

string to_string(){ ...
bool solve(string proposed_solution){ ...
    ^^^

To

string puzzle::to_string(){ ...
bool puzzle::solve(string proposed_solution){ ...

Upvotes: 3

Stephan Lechner
Stephan Lechner

Reputation: 35154

Your function bool solve(string proposed_solution) does not define a member function of puzzle but a "plain" function; Hence, get_solution(); within its body is not recognized as a member of puzzle, too. You'll have to write bool puzzle::solve(string proposed_solution) { ... and it should work.

Upvotes: 2

Related Questions