Function not recognized within scope

TheFaceOfBoe

I've almost finished writing a program that will detect palindromes from a file and output a new file highlighting the palindromes but I'm stuck on a really dumb error. I'm trying to write a test for one of my methods (TDD) and, for some reason, it's not recognizing the function as within the scope.

I'm calling the isPalindrome(string s) method (declared in PalindromeDetector.h) in my isPalindromeTest() method (declared in PalindromeDetectorTest.h) but, for some reason, it's not recognizing it as within the scoope.

I feel like everything should be working but it just isn't. Any help you can provide would be greatly appreciated. Below is my code:

PalindromeDetector.h

#ifndef PALINDROMEDETECTOR_H_
#define PALINDROMEDETECTOR_H_

#include <iostream>

using namespace std;

class PalindromeDetector {
public:
void detectPalindromes();
bool isPalindrome(string s);
};

#endif /* PALINDROMEDETECTOR_H_ */

PalindromeDetector.cpp

#include "PalindromeDetector.h"
#include "Stack.h"
#include "ArrayQueue.h"
#include <iostream>
#include <fstream>
#include <cassert>
#include <cctype>
#include <string>

using namespace std;

void PalindromeDetector::detectPalindromes() {
    cout << "Enter the name of the file whose palindromes you would like to detect:" << flush;
    string fileName;
    cin >> fileName;
    cout << "Enter the name of the file you would like to write the results to: " << flush;
    string outFileName;
    cin >> outFileName;
    fstream in;
    in.open(fileName.c_str());
    assert(in.is_open());
    ofstream out;
    out.open(outFileName.c_str());
    assert(out.is_open());
    string line;
    while(in.good()){
        getline(in, line);
        line = line.erase(line.length()-1);
        if(line.find_first_not_of(" \t\v\r\n")){
            string blankLine = line + "\n";
            out << blankLine;
        } else if(isPalindrome(line)){
            string palindromeYes = line + " ***\n";
            out << palindromeYes;
        } else {
            string palindromeNo = line + "\n";
            out << palindromeNo;
        }
        if(in.eof()){
            break;
        }
    }
    in.close();
    out.close();
}

bool PalindromeDetector::isPalindrome(string s){
    unsigned i = 0;
    Stack<char> s1(1);
    ArrayQueue<char> q1(1);
    while(s[i]){
        char c = tolower(s[i]);
        if(isalnum(c)){
            try{
                s1.push(c);
                q1.append(c);
            } catch(StackException& se) {
                unsigned capS = s1.getCapacity();
                unsigned capQ = q1.getCapacity();
                s1.setCapacity(2*capS);
                q1.setCapacity(2*capQ);
                s1.push(c);
                q1.append(c);
            }
        }
        i++;
    }
    while(s1.getSize() != 0){
        char ch1 = s1.pop();
        char ch2 = q1.remove();
        if(ch1 != ch2){
            return false;
        }
    }
    return true;
}

PalindromeDetectorTest.h

#ifndef PALINDROMEDETECTORTEST_H_
#define PALINDROMEDETECTORTEST_H_

#include "PalindromeDetector.h"

class PalindromeDetectorTest {
public:
    void runTests();
    void detectPalindromesTest();
    void isPalindromeTest();
};

#endif /* PALINDROMEDETECTORTEST_H_ */

PalindromeDetectorTest.cpp

#include "PalindromeDetectorTest.h"
#include <cassert>
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>

using namespace std;

void PalindromeDetectorTest::runTests(){
    cout << "Testing palindrome methods... " << endl;
    detectPalindromesTest();
    isPalindromeTest();
    cout << "All tests passed!\n" << endl;
}

void PalindromeDetectorTest::detectPalindromesTest(){
    cout << "- testing detectPalindromes()... " << flush;
    fstream in;
    string fileName = "testFile.txt";
    in.open(fileName.c_str());
    assert(in.is_open());
    cout << " 1 " << flush;
    ofstream out;
    string fileOutName = "testFileOut.txt";
    out.open(fileOutName.c_str());
    assert(out.is_open());
    cout << " 2 " << flush;


    cout << " Passed!" << endl;
}

void PalindromeDetectorTest::isPalindromeTest(){
    cout << "- testing isPalindrome()... " << flush;
    // test with one word palindrome
    string s1 = "racecar";
    assert(isPalindrome(s1) == true);      // these are not recognized within the scope
    cout << " 1 " << flush;
    // test with one word non-palindrome
    string s2 = "hello";
    assert(isPalindrome(s2) == false);    // these are not recognized within the scope
    cout << " 2 " << flush;
    // test with sentence palindrome
    string s3 = "O gnats, tango!";
    assert(isPalindrome(s3) == true);    // these are not recognized within the scope
    cout << " 3 " << flush;
    // test with sentence non-palindrome
    string s4 = "This is not a palindrome.";
    assert(isPalindrome(s4) == false);    // these are not recognized within the scope
    cout << " 4 " << flush;

    cout << " Passed!" << endl;
}
dlf

isPalindrome is a member function of PalindromeDetector, but you are trying to call it from within a PalindromeDetectorTest method. If the test class derived from PalindromeDetector this would work, but there isn't (and almost certainly shouldn't be) any such relationship between them.

You need a PalindromeDetector object to call the method on. Probably just as simple as this:

void PalindromeDetectorTest::isPalindromeTest(){
    cout << "- testing isPalindrome()... " << flush;

    PalindromeDetector sut; // "subject under test"

    // test with one word palindrome
    string s1 = "racecar";
    assert(sut.isPalindrome(s1) == true);
    // etc.
}

You could also make the PalindromeDetector methods static since the object doesn't appear to have any state. Then you could simply call PalindromeDetector::isPalindrome(s1); without the need to create an instance.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Scope within an anonymous function?

variable of function within a function is not being recognized

PHP Variable scope within function

scope of variable within a function in python

When is Lexical Scope for a function within a function determined?

Tcl/Tk: scope of variables for a function within a function

Unable to call function from within class - Function not recognized

$scope variable isn't recognized when calling initialization function

Modifying the $scope in a callback within a controller function

How to reference function property within an inner scope

Global variables are undefined within function scope

this.setState is not a function but should be within scope

Explanation of PHP array foreach scope within function

How to redefine a standard function within a scope in Racket?

How to execute a function within the scope of a predicate

Is it possible to declare/implement a template within a function scope?

point of declaration within function prototype scope in C

Global variable only within scope of R function

Add to array only within scope of function

Accessing scope within link function in Angular

Pass array to function or make sure it is within scope?

Access class scope from within anon function

Accessing php variable from within function scope

Scope chain in Javascript and calling nested function within the global scope

Callback function cannot access variable within parent function's scope

What is the scope of a PHP function defined within a PHP anonymous function?

Access array element from parent scope within inner function

How to call an outer scope function from within a JS module class

intelliJ -> is there any way to scope your search to be within a function

TOP Ranking

HotTag

Archive