Thread: Problem Compiling Program

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    48

    Problem Compiling Program

    Hi, I'm writing a program that reads in a string and removes the vowels from that string. For that, I wrote a function, known as compress(string& w), which removes the vowels. I'll show the header file, the main function, and the compress function. After that I will display the error message that it is generating. Thanks for any possible help.

    The Header File "compress.h"

    Code:
    void compress(string& w);
    The Main Program

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    #include "compress.h"
    
    
    int main() 
    {
    string word;
    vector<string> words;
        
    //Reads and removes vowels
    while (cin >> word) 
    {
    compress(word);
    words.push_back(word);
    }
        
    //Sorts the words in alphabetical order
    sort(words.begin(), words.end());
        
    
    for (int i=0; i<words.size(); i++) 
    {
    cout << words[i] << endl;
    }
    
    return (0);
    }
    Finally, the Function

    Code:
    #include <string>
    using namespace std;
    
    #include "compress.h"
    
    const char VOWELS[] = "aeiou";
    const int VMAX = 5;
    
    
    void compress(string& w) {
    string temp = w;
        
     
        
    //... Iterate character-by-character over each string.
    for (int i=w.size()-1; i >= 0; i--) {
    
    //... Iterate over each of the vowel characters.
    for (int v=0; v<VMAX; v++) {
    if (w[i] == VOWELS[v]) {              // When it's a vowel
    w.erase(i, 1);                               // Remove it.
    break;                                          // Try next word character.
    }
    }
    } 
    }
    This is the error message I am getting...

    Code:
    1>------ Rebuild All started: Project: Number 1B, Configuration: Debug Win32 ------
    1>Deleting intermediate and output files for project 'Number 1B', configuration 'Debug|Win32'
    1>Compiling...
    1>Number 1B.cpp
    1>e:\visual studio 2005\projects\homework 8\number 1b\number 1b\number 1b.cpp(23) : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
    1>        c:\program files\microsoft visual studio 8\vc\include\istream(1137): could be 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,signed char *)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\istream(1139): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,signed char &)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\istream(1141): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,unsigned char *)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\istream(1143): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,unsigned char &)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\istream(155): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_istream<_Elem,_Traits> &(__cdecl *)(std::basic_istream<_Elem,_Traits> &))'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\istream(161): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\istream(168): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::ios_base &(__cdecl *)(std::ios_base &))'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\istream(175): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::_Bool &)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\istream(194): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(short &)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\istream(228): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned short &)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\istream(247): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(int &)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\istream(273): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned int &)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\istream(291): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(long &)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\istream(309): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(__w64 unsigned long &)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\istream(329): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(__int64 &)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\istream(348): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned __int64 &)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\istream(367): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(float &)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\istream(386): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(double &)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\istream(404): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(long double &)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\istream(422): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(void *&)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        c:\program files\microsoft visual studio 8\vc\include\istream(441): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_streambuf<_Elem,_Traits> *)'
    1>        with
    1>        [
    1>            _Elem=char,
    1>            _Traits=std::char_traits<char>
    1>        ]
    1>        while trying to match the argument list '(std::istream, std::string)'
    1>e:\visual studio 2005\projects\homework 8\number 1b\number 1b\number 1b.cpp(23) : fatal error C1903: unable to recover from previous error(s); stopping compilation
    1>compress.cpp
    1>e:\visual studio 2005\projects\homework 8\number 1b\number 1b\compress.cpp(26) : warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data
    1>Generating Code...
    1>Build log was saved at "file://e:\Visual Studio 2005\Projects\Homework 8\Number 1B\Number 1B\Debug\BuildLog.htm"
    1>Number 1B - 2 error(s), 1 warning(s)
    ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
    Sorry for all of this, but I wanted to be thorough...
    Any help is greatly appreciated!

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    do you #include <string> ?

    and can your compress function handle strings with multiple vowels in a row? ("feel" ? When you erase that first e, everything will shift one)

    Also: Indentation will make your code a thousand times more readable. Invest in some whitespace.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    Thanks is was that I just forgot to #include <string>

    Also, to get my program to stop accepting inputs I have to include a sentinel value. I've worked with these before, but I'm still not 100&#37; sure of how to implement, any help?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Simple:

    Code:
    while (cin >> word && word != sentinel)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    Thanks, got the program running correctly now!

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Argh, don't use "using namespace std;"

    If you DO choose to do this, include all header files BEFORE it!

    "using namespace" completely changes the meaning of all code following it, including code from header files.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    I'm in an entry level C++ programming class at Penn State University, my professor has always instructed us to use 'using namespace std;' although he has never explained it's purpose or use. However, the program ran correctly, so what affect would including the header file before or after the 'namespace' have?

    Thanks for the insight.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    However, the program ran correctly, so what affect would including the header file before or after the 'namespace' have?
    All the names from the std namespace in those standard headers included would have been introduced into the current file's scope. This increases the chance of a naming conflict. You may be able to spot and work around the name conflict in the current file, but since this is propagated to other header files that were included after the using directive, you may end up with a big mess.
    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

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    If he simply moves "using namespace std" after the includes, the program won't compile, because his prototypes use standard containers.

    I recommend keeping the practice of putting "using namespace std" before headers, until you learn about namespaces and what the using directive does. Alternatively, you can learn all that now, so that you're not practicing a bad practice.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by King Mir View Post
    I recommend keeping the practice of putting "using namespace std" before headers, until you learn about namespaces and what the using directive does. Alternatively, you can learn all that now, so that you're not practicing a bad practice.
    So his header file is broken. It doesn't work unless the user knows to put "using namespace std;" before including it? Very poor, IMO.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If compress.h needs the using directive, it should have the using directive inside it. It should not rely on source files that include it to have a using directive before its inclusion. It should also #include <string> for the same reason.

    It's not good practice to have a using directive in a header file, though, so a simple solution would be to #include <string> and refer to it as std::string inside compress.h.

    For a small program like this, I don't think it's a big deal, though, especially if that's what the instructor prefers. Just know that it's generally considered bad practice to have a using directive in a header or in a source file before header includes.

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Daved View Post
    If compress.h needs the using directive, it should have the using directive inside it. It should not rely on source files that include it to have a using directive before its inclusion. It should also #include <string> for the same reason.
    I could argue about this, but you're right. It's a small piece of code, written for an instructor who may not have ALL his marbles. To the original poster -- keep it in mind. "using namespace" is not something you typically should be using.

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by wco5002 View Post
    I'm in an entry level C++ programming class at Penn State University
    Damn PSU'ers! You guys are the reason my alma mater of Portland State University has to go by the acronym "PDX" instead of "PSU!" To hell with you people!

    I kid!

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    For a small program like this, I don't think it's a big deal, though, especially if that's what the instructor prefers.
    To be fair, we do not really know what the professor prefers. Perhaps he did not expect his students to place the using directive before including a header, but he did not include negative examples concerning this so some students, like wco5002, did so.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem w/ doubles in friend's program
    By mkylman in forum C Programming
    Replies: 16
    Last Post: 11-22-2008, 10:45 AM
  2. Problem with my program i cant figure out...
    By youareafever in forum C Programming
    Replies: 7
    Last Post: 11-01-2008, 11:56 PM
  3. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM
  4. problem with 2d array program.
    By Niloc1 in forum C Programming
    Replies: 1
    Last Post: 04-08-2002, 05:47 PM
  5. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM