指针问题2 [有效的C++语法]

use*_*120 0 c++ pointers

这个版本正在运行.我在//整个代码中都发表了评论,以便更好地说明我遇到的问题.该程序依赖于读取文本文件.包含标点符号的段落格式.

可以将此和上述内容复制到文本文件中并运行该程序.

// Word.cpp

#define _CRT_SECURE_NO_WARNINGS // disable warnings for strcpy
#define ARRY_SZ 100
#include <iostream>
#include <fstream>
#include "Word.h"

using namespace std;

Word::Word( const char* word )
{
    ptr_ = new char[ strlen( word ) + 1 ];
    strcpy( ptr_, word  );  
    len_ = strlen( ptr_ );
}

Word::Word( const Word* theObject ) 
{
    ptr_ = theObject->ptr_;
    len_ = theObject->len_;
}

Word::~Word()
{
    delete [] ptr_;
    ptr_ = NULL;
}

char Word::GetFirstLetterLower()
{
    // I want to use ptr_ and len_ here
    // but the information is gone!
    char c = 0;
    return c;
}

char* Word::GetWord()
{
    for (int x = 0; x < strlen( (char*)ptr_ ); x++)
        ptr_[x];  // Results in a crash.

    return ptr_;
}
Run Code Online (Sandbox Code Playgroud)

// Word.h

const int FILE_PATH_SZ = 512;
class Word
{
private:
    char* ptr_;
    int len_;
public:
    Word( const Word* ); // an appropriate default constructor
    Word( const char* );
    ~Word( );
    char GetFirstLetterLower( );
    char* GetWord( );
    static char fileEntry[ FILE_PATH_SZ ];
};
Run Code Online (Sandbox Code Playgroud)

// main.cpp

#ifdef  _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <iostream>
#include <fstream>
#include <string>
#endif
#include "Word.h"
using namespace std;

const int WORD_SZ = 100;
Word** g_wordArray;
int g_arrSz;

static char filePath[ FILE_PATH_SZ ] = {};
void FreeWordArray();

int main( const int argc, const char **argv )
{
    int     wrdCount = 0;
    char    usrMenuOption     = 0,
            getFirstLetter          = 0,
            tmpArray[WORD_SZ] = {},
            *getWord = 0;
    string  str, 
            str2;
    ifstream  inFile, 
              inFile2;

    do 
    {
        cout << "Please make a selection: \n\
a) Read a text file\n\
b) Remove words starting with letter\n\
c) Print words to console\n\
d) Quit\n";
        cin  >> usrMenuOption;
        switch( usrMenuOption )
        {
            case'A':
            case'a':
                cout << "Enter a file name: ";
                cin.sync();
                cin  >> filePath;
                inFile.open( filePath );
                if ( !inFile ) return -1;
                inFile >> str; // prime the eof flag
                while ( !inFile.eof() )
                {        
                    inFile >> str;
                    wrdCount++;
                    g_wordArray = new Word *[wrdCount];
                }
                inFile.close();
                inFile2.open( filePath );

                while( !inFile2.eof() )
                {
                    inFile2 >> str2;
                    for ( unsigned x = 0; x < str2.length(); x++ )
                    g_wordArray[x] = new Word( str2.c_str() );
                }
                cout << wrdCount << " Words read from the file " << endl;
                inFile2.close();
                break;
            case'B':
            case'b':
                getFirstLetter = g_wordArray[wrdCount]->GetFirstLetterLower();
                //getWord = g_wordArray[wrdCount]->GetWord();
                cout << getWord << endl;
                break;
            case'C':
            case'c':
                break;
            case'D':
            case'd':
                cout << "Quit Requested. " << endl;
                break;
            default:
                cout << '"' << usrMenuOption << '"' << " Not Defined! " << endl;
        }
    } while (usrMenuOption != 'D' && usrMenuOption != 'd');

#ifdef _DEBUG
    _CrtDumpMemoryLeaks();
#endif
    cin.ignore();
    return 0;
}

void FreeWordArray()
{
    // free the memory that is allocated
    return;
}
Run Code Online (Sandbox Code Playgroud)

Eva*_*ran 9

编辑:我把这个编辑放在顶部,因为它直接回答了你为什么Word被破坏的问题.您的副本构造函数是错误的:

Word::Word( const Word* theObject ) 
{
    ptr_ = theObject->ptr_;
    len_ = theObject->len_;
}
Run Code Online (Sandbox Code Playgroud)

这不会复制theObject->ptr_指向的内容,只是指针.因此,您有效地将两个Word对象指向同一个内部字符串.当Word对象被删除时,这会非常糟糕.一个正确的实现(使用你所做的技术,我建议反对他们)将是这样的:

Word::Word( const Word* theObject ) 
{
    ptr_ = new char[theObject->len_ + 1 ];
    strcpy( ptr_, theObject->ptr_  );  
    len_ = theObject->len_;
}
Run Code Online (Sandbox Code Playgroud)

编辑: Earwicker也注意到以下内容:

...虽然"复制构造函数"不是复制构造函数.因此编译器生成的将仍然存在,并执行相同的成员复制,因此仍存在相同的问题.

要解决这个问题,你需要制作一个应该具有原型的正确的复制构造函数:

Word::Word(const Word &theObject);
Run Code Online (Sandbox Code Playgroud)

这段代码也在这里:

while ( !inFile.eof() )
{        
    inFile >> str;
    wrdCount++;
    g_wordArray = new Word *[wrdCount];
}
Run Code Online (Sandbox Code Playgroud)

像筛子一样泄漏!g_wordArray在读完每个单词后重新分配,完全忘记删除前一个单词.我将再一次使用您尝试使用的技术展示一个理智的实现.

while (inFile >> str)
{        
    inFile >> str;
    wrdCount++;
}
g_wordArray = new Word *[wrdCount];
Run Code Online (Sandbox Code Playgroud)

注意它如何计算单词,然后在知道要分配多少之后分配空间一次.现在g_wordArray已准备好用于最多wrdCount单词对象.

原始答案:

你为什么不用这个Word班级替换std::string?它会使代码更小,更容易使用.

如果它让你更容易,只需这样做:

typedef std::string Word;
Run Code Online (Sandbox Code Playgroud)

然后你可以这样做:

Word word("hello");
char first_char = word[0];
Run Code Online (Sandbox Code Playgroud)

加上它还有额外的好处,你无需使用该.c_str()成员来获得c风格的字符串.

编辑:

我也将改变你的g_wordArray只是成为一个std::vector<Word>.这样你可以简单地做到这一点:

g_wordArray.push_back(Word(str));
Run Code Online (Sandbox Code Playgroud)

没有更多动态分配!这一切都是为你完成的.单词数组的大小将仅受您拥有的RAM数量的限制,因为std::vector您在使用时会根据需要增长push_back().

此外,如果你这样做...猜猜得到单词计数你只需这样做:

g_wordArray.size();
Run Code Online (Sandbox Code Playgroud)

无需手动跟踪它们的数量!

编辑:

此外,此代码已被破坏:

while( !inFile2.eof() )
{
    inFile2 >> str2;
    ...
}
Run Code Online (Sandbox Code Playgroud)

因为尝试读取之后才设置eof ,所以最好使用此模式:

while(inFile2 >> str2)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

这将在EOF上正确停止.

最重要的是,如果你这样做,你应该写的实际代码非常少.

编辑:

这是我想你想要的直接实现示例.从菜单项看,似乎用户首先选择选项'a',然后'b'零次或多次过滤掉一些单词,然后最后c打印结果(每行一个单词).此外,实际上不需要选项'D',因为命中Ctrl+D发送EOF到程序并使" while(std::cin >> option)"测试失败.从而结束了该计划.(至少在我的操作系统中,它可能是Windows的Ctrl + Z`).

此外,它没有做任何努力(也没有你的)处理标点符号,但这里是:

#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <functional>
#include <iterator>

struct starts_with : public std::binary_function<std::string, char, bool> {
    bool operator()(const std::string &s, char ch) const {
        return s[0] == ch;
    }
};

void print_prompt() {
    std::cout << "Please make a selection: \na) Read a text file\nb) Remove words starting with letter\nc) Print words to console" << std::endl;
}

int main( const int argc, const char **argv) {
    std::vector<std::string> file_words;
    char option;
    print_prompt();
    while(std::cin >> option) {
        switch(option) {
        case 'a':
        case 'A':
            std::cout << "Enter a file name: ";
            // scope so we can have locals declared
            {
                std::string filename;
                std::string word;
                std::cin >> filename;
                int word_count = 0;
                std::ifstream file(filename.c_str());
                while(file >> word) {
                    file_words.push_back(word);
                }
                std::cout << file_words.size() << " Words read from the file " << std::endl;
            }
            break;
        case 'b':
        case 'B':
            // scope so we can have locals declared
            {
                std::cout << "Enter letter to filter: ";
                char letter;
                std::cin >> letter;

                // remove all words starting with a certain char
                file_words.erase(std::remove_if(file_words.begin(), file_words.end(), std::bind2nd(starts_with(), letter)), file_words.end());
            }
            break;          

        case 'c':
        case 'C':
            // output each word to std::cout separated by newlines
            std::copy(file_words.begin(), file_words.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
            break;
        }
        print_prompt();
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)