+ -
当前位置:首页 → 问答吧 → 麻烦帮看下这个程序错在什么地方

麻烦帮看下这个程序错在什么地方

时间:2011-11-29

来源:互联网

//StrType.h#include <fstream>#include <iostream>#include <cstring> const int MAX_CHARS=100;enum InType {ALPHA_NUM, ALPHA, NON_WHITE, NOT_NEW}; class StrType{public: void MakeEmpty(); void GetString(bool skip, InType charsAllowed); void GetStringFile(bool skip, InType charsAllowed, std::ifstream& inFile); void PrintToScreen(bool newLine); void PrintToFile(bool newLine, std::ofstream& outFile); int LenghtIs(); void CopyString(StrType& newString); bool operator==(const StrType& rhs) const { return strcmp(letters, rhs.letters) == 0; } bool operator<(const StrType& rhs) const { return strcmp(letters, rhs.letters) < 0; }private: char letters[MAX_CHARS + 1];}; void StrType::MakeEmpty(){ letters[0] ='\0';}//StrType.h
#include <fstream>
#include <iostream>
#include <cstring>

const int MAX_CHARS=100;
enum InType {ALPHA_NUM, ALPHA, NON_WHITE, NOT_NEW};

class StrType
{
public:
  void MakeEmpty();
  void GetString(bool skip, InType charsAllowed);
  void GetStringFile(bool skip, InType charsAllowed,
  std::ifstream& inFile);
  void PrintToScreen(bool newLine);
  void PrintToFile(bool newLine, std::ofstream& outFile);
  int LenghtIs();
  void CopyString(StrType& newString);
  bool operator==(const StrType& rhs) const
  {
  return strcmp(letters, rhs.letters) == 0;
  }
  bool operator<(const StrType& rhs) const
  {
  return strcmp(letters, rhs.letters) < 0;
  }
private:
  char letters[MAX_CHARS + 1];
};

void StrType::MakeEmpty()
{
  letters[0] ='\0';
}

 //main.cpp#include <fstream>#include "StrType.h"#include <cstddef>#include <iostream>#include <string>using namespace std; struct WordType{ public: StrType word; int count;}; struct TreeNode{ WordType info; TreeNode* left; TreeNode* right;}; class ListType{ public: ListType(); void InsertOrIncrement (StrType string); void Print(std::ofstream&) const; private: TreeNode* root;}; ListType::ListType(){ root=NULL;} void Process(TreeNode*& tree, StrType s){ if(tree == NULL) { tree = new TreeNode; tree->info.word = s; tree->info.count = 1; tree->left = NULL; tree->right = NULL; } else if (tree->info.word == s) tree->info.count++; else if (s < tree->info.word) Process(tree->left, s); else Process(tree->right, s);} void ListType::InsertOrIncrement(StrType s){ Process(root, s);} void Print (TreeNode* tree, std::ofstream& outFile){ if (tree!= NULL) { Print(tree->left, outFile); tree->info.word.PrintToFile(true, outFile); outFile <<" "<< tree->info.count; Print(tree->right, outFile); }} void ListType::Print(std::ofstream& outFile) const{ ::Print(root, outFile);} int main(){ using namespace std; ListType list; string inFileName; string outFileName; string outputLabel; ifstream inFile; ofstream outFile; StrType string; int minimumLenght; cout<<"enter in imput file name."<<endl; cin>>inFileName; inFile.open(inFileName.c_str()); cout<<"enter name of output file."<<endl; cin>>outFileName; outFile.open(outFileName.c_str()); cout<<"enter name of test run."<<endl; cin>>outputLabel; outFile<< outputLabel << endl; cout<<"enter the min word size."<<endl; cin>>minimumLenght; string.GetStringFile(true, ALPHA_NUM, inFile); while(inFile) { if(string.LenghtIs() >= minimumLenght) list.InsertOrIncrement(string); string.GetStringFile(true, ALPHA_NUM, inFile); } list.Print(outFile); outFile.close(); inFile.close(); return 0;} //main.cpp
#include <fstream>
#include "StrType.h"
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;

struct WordType
{
  public:
  StrType word;  
  int count;
};

struct TreeNode
{
  WordType info;
  TreeNode* left;
  TreeNode* right;
};

class ListType
{
  public:
  ListType();
  void InsertOrIncrement (StrType string);
  void Print(std::ofstream&) const;
  private:
  TreeNode* root;
};


ListType::ListType()
{
  root=NULL;
}

void Process(TreeNode*& tree, StrType s)
{
  if(tree == NULL)
  {
  tree = new TreeNode;
  tree->info.word = s;
  tree->info.count = 1;
  tree->left = NULL;
  tree->right = NULL;
  }
  else if (tree->info.word == s)
  tree->info.count++;
  else if (s < tree->info.word)
  Process(tree->left, s);
  else 
  Process(tree->right, s);
}

void ListType::InsertOrIncrement(StrType s)
{
  Process(root, s);
}

void Print (TreeNode* tree, std::ofstream& outFile)
{
  if (tree!= NULL)
  {
  Print(tree->left, outFile);
  tree->info.word.PrintToFile(true, outFile);
  outFile <<" "<< tree->info.count;
  Print(tree->right, outFile);
  }
}

void ListType::Print(std::ofstream& outFile) const
{
  ::Print(root, outFile);
}
   

int main()
{
  using namespace std;
  ListType list;
  string inFileName;
  string outFileName;
  string outputLabel;
  ifstream inFile;
  ofstream outFile;
  StrType string;
  int minimumLenght;
   
  cout<<"enter in imput file name."<<endl;
  cin>>inFileName;
  inFile.open(inFileName.c_str());
   
  cout<<"enter name of output file."<<endl;
  cin>>outFileName;
  outFile.open(outFileName.c_str());
   
  cout<<"enter name of test run."<<endl;
  cin>>outputLabel;
  outFile<< outputLabel << endl;
   
  cout<<"enter the min word size."<<endl;
  cin>>minimumLenght;
   
  string.GetStringFile(true, ALPHA_NUM, inFile);
  while(inFile)
  {
  if(string.LenghtIs() >= minimumLenght)
  list.InsertOrIncrement(string);
  string.GetStringFile(true, ALPHA_NUM, inFile);
  }
   
  list.Print(outFile);
  outFile.close();
  inFile.close();
  return 0;
}



#include <fstream>
#include "StrType.h"
#include <iostream>
#include <string>
#include <cctype>
#include <cstring>





using namespace std;


// Prototypes of auxiliary function.
void GetAlphaNum(bool skip, char letters[]);
// Post: letters array contains only alphabetic characters and numbers.



void StrType::GetString(bool skip, InType charsAllowed)
{
  switch (charsAllowed)
  {
case ALPHA_NUM : GetAlphaNum(skip, letters);
break;
//commented out:
// case ALPHA : GetAlpha(skip, letters);
// break;
// case NON_WHITE : GetNonWhite(skip, letters);
// break;  
// case NOT_NEW : GetTilNew(skip, letters);
// break;
  }
}



void StrType::CopyString(StrType& newString)
{
  strcpy(newString.letters, letters);
}

void StrType::PrintToScreen(bool newLine)
{
  if (newLine)
cout << endl;
  cout << letters;
}

void StrType::MakeEmpty()
{
  letters[0] = '\0';
}

int StrType::LengthIs()
{
  return strlen(letters);
}


//note that this is NOT a member function of the StrType class.
//It's a global function that is called by some member functions.

void GetAlphaNum(bool skip, char letters[])
{
  char letter;
  int count = 0;

  if (skip)
  {
cin.get(letter);
while (!isalnum(letter) && cin)
cin.get(letter);
  }
  else
cin.get(letter);
  if (!cin || !isalnum(letter))
letters[0] = '\0';

  else
  {
do
{
letters[count] = letter;
count++;
cin.get(letter);
} while (isalnum(letter) && cin && (count < MAX_CHARS));
letters[count] = '\0';
// Skip extra characters if necessary.
if (count == MAX_CHARS)
do
{
cin.get(letter);
} while (isalnum(letter) && cin);
  }
}

错误是undefined reference to `StrType::PrintToFile(bool, std::basic_ofstream<char, st
d::char_traits<char> >&)'

undefined reference to `StrType::GetStringFile(bool, InType, std::basic_ifstrea
m<char, std::char_traits<char> >&)'
undefined reference to `StrType::LenghtIs()'
undefined reference to `StrType::GetStringFile(bool, InType, std::basic_ifstrea
m<char, std::char_traits<char> >&)'
collect2: ld returned 1 exit status

作者: ynqjxq66   发布时间: 2011-11-29

这样的代码,,。。。

作者: mingliang1212   发布时间: 2011-11-29