有人知道如何在多维数组中存储指针吗?我认为这可能是我在 main 中遇到的问题:
// 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 = new char;
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 >> tmpArray; // prime the eof flag.
while ( !inFile.eof() )
{
inFile >> tmpArray;
wrdCount++;
g_wordArray = new Word *[wrdCount];
}
inFile.close();
inFile2.open( filePath );
while( !inFile2.eof() )
{
inFile2 >> tmpArray;
// supplies the member functions with information from the file
g_wordArray[wrdCount] = new Word( tmpArray );
g_wordArray[wrdCount]->GetFirstLetterLower();
g_wordArray[wrdCount]->GetWord();
}
cout << wrdCount << " Words read from the file " << endl;
inFile2.close();
break;
case'B':
case'b':
// information not found returning null
g_wordArray[wrdCount]->GetFirstLetterLower();
break;
case'C':
case'c':
g_wordArray[wrdCount]->GetWord();
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()
{
delete[ ] g_wordArray;
return;
}
// Word.cpp
#define _CRT_SECURE_NO_WARNINGS // disable warnings for strcpy
#define ARRY_SZ 100
#include <iostream>
#include <fstream>
#include "Word.h"
#include <vector>
using namespace std;
// No paramaters.
// Is this what im missing?
// I just threw it in because of an error.
Word::Word()
{
}
Word::Word( const char* word )
{
ptr_ = new char[ strlen( word ) + 1 ];
strcpy( ptr_, word );
len_ = strlen( ptr_ );
}
Word::~Word()
{
delete[ ] ptr_;
ptr_ = NULL;
len_ = NULL;
}
char Word::GetFirstLetterLower()
{
char myChar = tolower( ptr_[0] );
return myChar;
}
char* Word::GetWord()
{
Word *objectOne = new Word;
objectOne->ptr_ = ptr_;
strcpy( objectOne->ptr_, ptr_ );
return objectOne->ptr_;
}
我的目标是在我的 g_wordArray[wrdCount]->SomeFunction()
中从文件中读取所有单词,而不依赖于文件读取循环。
我一直在努力做的事情:
在实现文件中,在
getFirstLetterLower
下:每次将char *_ptr
私有(private)成员添加到一个新变量中。像someCharVar[0] = firstWord
,someCharVar[1] = secondWord
...将文件内容读入单个变量。在我需要的每种情况下循环遍历该变量。
我喜欢这个想法,但还不知道如何去做。有什么建议吗?
最佳答案
请发布可以重现您的问题的最少代码。现在看起来很朦胧。我想这段代码有很多问题
inFile >> tmpArray; // prime the eof flag.
while ( !inFile.eof() )
{
inFile >> tmpArray;
wrdCount++;
g_wordArray = new Word *[wrdCount];
}
你在这里严重泄漏内存。先前分配的“g_wordArray”会怎样?
此外,当您分配“n”个元素时,您无法访问“第 n 个”索引。内存占用范围为 0 - (n-1)。
g_wordArray[wrdCount]
请重新检查代码,尝试调试,然后发布最少的代码。
关于c++ - 指针问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/764890/