2014-11-21 4 views
0

파일에서 문장 검색을 감지하고 문장을 강조하는 새 파일을 출력하는 프로그램이 거의 끝났습니다.하지만 나는 정말 바보 같은 오류에 빠져 있습니다. 내 방법 중 하나 (TDD)에 대한 테스트를 작성하려고하는데, 어떤 이유로, 범위 내에서 함수를 인식하지 못합니다.범위 내에서 기능이 인식되지 않습니다.

(PalindromeDetectorTest.h에 선언 된) isPalindromeTest() 메서드에서 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

+0

'while (in.good())'가 잘못되었으므로'while (getline (..)) '을 직접 사용해야합니다. –

답변

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);으로 간단하게 전화 할 수 있습니다.

관련 문제