Thread: Confused about constructors

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ok, so how do I go about that? What would that parameter be? And why couldn't I just open the file in the constructor rather than passing it from main and then opening it?
    You can do what you want. The question is, what do you want to do?

    a) Open the file and pass the opened stream to the constructor.

    b) Pass the filename to the constructor and open the file in the constructor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Sep 2007
    Posts
    36
    If I have to do one of those, I suppose I would rather pass the filename to the ocnstructor and open it there. How do I do that?

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How do I do that?
    Provide a const std::string& parameter.

    EDIT:
    In the constructor, you would use the c_str() function of std::string to pass to std::ifstream's or std::ofstream's constructor or open() member function to open the file.
    Last edited by laserlight; 03-05-2008 at 01:08 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Sep 2007
    Posts
    36
    Now I'm more ocnfused than ever! I need an object to call the return function, but now when I say something like "Scramble object" as a declaration, I get the error "No matching function call to Scramble object". Argh!

    Code:
    #include <iostream> 
    #include <string> 
    #include <fstream> 
    #include <vector>
    using namespace std; 
    
    class Scramble { 
          private: 
                   vector<string> _word;
                   bool Check (string T, string S); 
          public: 
                  Scramble(const std::string&, vector<string> words){  
                        ifstream file("dictionary.txt");
                        if(!file)
                        {
    	                    cout <<"\nError! The file couldn't be opened\n";
                        }
                        vector<string> word_list;
                        string word;
                        while(file >> word)
                        {
                            word_list.push_back(word);
                        }
                        _word=words;
                   }
                   void Descramble (string Scrambled); 
                   const std::vector< std::string > returnWord () 
                   {     
                         return _word;
                   }
            };
    //******************************************
    void Scramble::Descramble(string Scrambled)
    { 
          bool flag=false; 
          for(int i=0;i<_word.size();i++)
          { 
                  if(_word[i].size()==Scrambled.size())
                  {
                       if(Check(Scrambled,_word[i])) 
                       {
                            cout << "The word is: " << _word[i]<< endl; 
                            flag=true;}
                  }
          }
          if (!flag)
          {
                  cout<< "The word: " << Scrambled <<" is not found"<< endl; 
                  return;
          }
    }
    //******************************************
    bool Scramble::Check(string T, string S)
    { 
          int n=T.size(); 
          int Found=0; 
          string temp=S; 
          for (int i=0; i<n; i++) 
          { 
                  bool flag = true; 
                  for (int j=0; j < n && flag;j++) 
                  if(T[i]== temp[j])
                  { 
                       temp[j]=' ';
                       flag=false;Found ++;
                  } 
          }
          if (Found==n)
                  return true; 
          else 
                  return false;
    } 
    //******************************************
    int main()
    { 
          vector <string> word_list;
          cout << "Welcome to the Scramble Game!\n\n";
          while (true)
          {
                  int play;
                  cout << "\nWould you like to play? \nEnter 1 for Yes or 2 for No:\n";
                  cin >> play;
                  if (play==1)
                  {  
                      cout <<"Enter \n";
                      string Scrambled;
                      cin >> Scrambled;
                       word_list=word_list.Scramble::returnWord();
                      Scramble test(word_list); 
                      test.Descramble(Scrambled);
                      cin.get(); cin.get();
                  } 
                  else if (play==2)
                  {
                       cout << "\n\nGoodbye!\n\n";
                       cin.get(); cin.get();
                       return 0;
                  }
                  else
                  {
                       cout << "\nInvalid entry. Try again.\n\n";
                       cin.get(); cin.get();
                  }
          }
    }

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Take a look again at your class.
    It has only one constructor and it takes two arguments.
    But you are trying to create an object and passing only one argument.
    Pass both arguments or create a new constructor that takes only one argument.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. still confused with constructors and private data members
    By freddyvorhees in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2008, 09:29 AM
  2. Copy constructors; Best practices (and private)
    By Mario F. in forum C++ Programming
    Replies: 15
    Last Post: 06-23-2006, 04:42 PM
  3. constructors, arrays, and new
    By Thantos in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2004, 06:21 PM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM