功能未在范围内识别

时间:2014-11-21 01:38:08

标签: c++ scope include palindrome

我差不多完成了一个程序,它会检测文件中的回文并输出一个突出显示回文的新文件,但我却陷入了一个非常愚蠢的错误。我试图为我的一种方法(TDD)编写测试,并且由于某种原因,它没有将该功能识别为在范围内。

我在我的isPalindromeTest()方法中调用isPalindrome(string s)方法(在PalindromeDetector.h中声明)(在PalindromeDetectorTest.h中声明),但由于某种原因,它没有识别它就像在scoope中一样。

我觉得一切都应该有效,但事实并非如此。您将提供的任何帮助将不胜感激。以下是我的代码:

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;
}

1 个答案:

答案 0 :(得分:0)

isPalindromePalindromeDetector的成员函数,但您尝试在PalindromeDetectorTest方法中调用它。如果从PalindromeDetector派生的测试类可以使用,但是它们之间没有(并且几乎肯定不应该)这样的关系。

您需要一个PalindromeDetector对象来调用该方法。可能就像这样简单:

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.
}

您还可以将PalindromeDetector方法设为静态,因为该对象似乎没有任何状态。然后你可以简单地调用PalindromeDetector::isPalindrome(s1);而无需创建实例。