Thread: Stringstream problem

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    27

    Stringstream problem

    Hey there im having a problem trying to compile my code, i get :

    Code:
    C:\money.cpp variable `std::stringstream ss' has initializer but incomplete type
    Im not totally sure why, i've tried googling about but am still stumped by this, code:


    money.h:
    Code:
    #ifndef MONEY_H
    #define MONEY_H
    using namespace std;
    class parse{
        private:
            string input;
        public:
            parse();
            ~parse();
            double parseIn(string& unparse);
    };
    #endif
    money.cpp:
    Code:
    #include <iostream>
    #include <string>
    #include "money.h"
    
    parse::parse(){
        input = "0";
    }
    
    parse::~parse(){
        string *ptr;
        ptr = &input;
        delete ptr;
    }
    
    double parse::parseIn(string& inStr){
        stringstream ss(inStr);
        double out;
        ss >> out;
        return out;
    }
    main.cpp:
    Code:
    #include <iostream>
    #include <string>
    #include "money.h"
    
    int main()
    {
        string in;
        parse testParse;
        cout << "enter stuff: " << endl;
        cin >> in;
        testParse.parseIn(in);
        cout << "You entered: " << in << endl;
        testParse.~parse();
        return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There is no such thing as std::stringstream unless and until you #include <sstream>.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    Ahh yup sorry, my bad

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    parse::~parse(){
        string *ptr;
        ptr = &input;
        delete ptr;
    }
    Code:
      testParse.~parse();
    What are you doing there? It's not relevant to your initial question, but that code makes no sense and is wrong in several ways. If you describe what you're trying to accomplish then perhaps an alternative can be suggested. In general, simply deleting the code I posted from your original code would be your best bet.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    Yeah sorry im having a hard time trying to understand some of these concepts, and im under alot of pressure atm, either way im trying to write some code that will take in strings and strip the integers(or doubles) out of the string to be able to perform mathmatic operaions on(working out a cars tow load etc etc), not working out how i want it too though.

    I thought this code:

    Code:
    double parse::parseIn(string& inStr){
        stringstream ss(inStr);
        double out;
        ss >> out;
        if(ss.fail()){
            throw (inStr + " is not a number");
        }
        return out;
    }
    would do just that (parse numbers from words that is), but its not happening, the returned value is still a string type because i cant do work with it via operators(minus, multiply etc)

    Code:
    int main()
    {
        string in1, in2;
        double out;
        parse testParse;
        cout << "enter stuff: ";
        cin >> in1;
        try{
            testParse.parseIn(in1);
        }
        catch(string& e){
            cerr << e << endl;
        }
        cout << "enter another number: ";
        cin >> in2;
        try{
              testParse.parseIn(in2);
         }
         catch(string& e){
              cerr << e << endl;
         }
         out = in1+in2;
         cout << out << endl;
        cin.ignore();
        cin.get();
        return 0;
    will result in:

    Code:
    cannot convert `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to `double' in assignment

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    out = in1+in2;
    Well obviously, out is a double and in1 & in2 are std::strings. You can't put a square peg in a round hole.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You need to store the return value of the call to parseIn for both inputs. Then use those values instead of in1 and in2 when you add them together and assign to out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stringstream to float problem
    By c0d3m0nk3y in forum C++ Programming
    Replies: 10
    Last Post: 02-01-2008, 04:07 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM