2014-11-21 105 views
0

我几乎已经完成了一个程序,它能够检测文件中的回文并输出一个突出显示回文的新文件,但是我被困在一个非常愚蠢的错误中。我试图为我的一个方法(TDD)编写一个测试,并且出于某种原因,它不能在范围内识别该函数。功能在范围内不被识别

我在我的isPalindromeTest()方法(在PalindromeDetectorTest.h中声明)中调用了isPalindrome(string s)方法(在PalindromeDetector.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

'isPalindrome'是'PalindromeDetector'的成员函数。你需要一个“PalindromeDetector”对象来调用它。 – dlf 2014-11-21 01:43:18

+0

'while(in.good())'是错的,你应该直接使用'while(getline(..))'。 – 2014-11-21 01:46:36

回答

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);而不需要创建一个实例。