Confused about constructors

This is a discussion on Confused about constructors within the C++ Programming forums, part of the General Programming Boards category; Ok, so how do I go about that? What would that parameter be? And why couldn't I just open the ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,593
    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 12:08 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    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.

Page 2 of 2 FirstFirst 12
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, 09:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21