Thread: Compiling

  1. #1
    Registered User
    Join Date
    Aug 2008

    Question Compiling

    I was sent some C++ code that will be used to process files, by creating an object that takes two arguments: one is a string with the path for a (local) file, the other a stringstream where the result will be stored.
    Since there was no main in the code I received, I have written my own - but when compiling (g++ in ubuntu) there's this error I can not solve:
    error: conversion from ‘std::stringstream*’ to non-scalar
     type ‘std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >’ requested
    The main I wrote:

    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <string>
    #include "emailParser.h"
    using namespace std;
    int main (int argc, char *argv[]) {
      string myfile = (string)argv[1];
      stringstream ftw = new stringstream();
      EmailParser(myfile, ftw);
      return 0;
    The person who I got the code from is away in holidays and unfortunately, unreachable, so if anyone could point me out what I am doing wrong, I'd be much appreciated

    I can provide the full code if it helps
    Last edited by laz; 08-07-2008 at 08:36 AM. Reason: page layout not so lengthy; full code helps?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Are you a Java programmer, by any chance? The first most obvious problem is:
    stringstream ftw = new stringstream();
    new stringstream() returns a pointer to a stringstream, which clearly is not a stringstream. You would likely want to just write:
    stringstream ftw;
    Also, instead of casting argv[1] to string, you would likely want to just initialise myfile with argv[1], e.g.,
    string myfile(argv[1]);
    Of course, you should check that argc is greater than 1 before using argv[1].
    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

  3. #3
    Registered User
    Join Date
    Aug 2008
    First of all, thank you for your reply.
    Second, you were right. Even though I am not a programmer, I do a little coding in Java, and even though I did look for c++ manuals and tuts in the web, somehow I failed to understand I would be creating a pointer and not the object.
    So now I have a different error, but it seems it is a linking problem...
    labrisio@la-ux:~$ g++ Metpreproc.cpp -o Meto
    /tmp/ccuY3Pir.o: In function `main':
    Metpreproc.cpp:(.text+0x153): undefined reference to `EmailParser::EmailParser(std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
     std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >&)'
    Metpreproc.cpp:(.text+0x15e): undefined reference to `EmailParser::~EmailParser()'
    collect2: ld returned 1 exit status
    .. according to this fella.

    So, now I'm looking at a page on how to create makefiles and such and wondering if I was doing the compiling wrong? Do all c++ programs require linking and makefiles?
    Sorry for all the questions..
    Last edited by laz; 08-07-2008 at 09:57 AM. Reason: same lenghty stuff..

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Not all programs require makefiles, no. But they're handy to have if your project gets at all large.

    At the command line, you need to type every .cpp file involved before the -o (I'm guessing your other one is emailParser.cpp). Otherwise the functions defined in that .cpp file aren't available for your finished product.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    ALL C and C++ programs require linking to produce the final executable, since the code generated by the compiler needs to be combined with the C runtime library if nothing else. In this case, you probably also have a emailparser source file that you need to include.

    Makefiles is a way to automate the process of building non-trivial sets of source-code into an executable file.

    In this case, it is probably not necessary to have a makefile, but just add the other source file to the list of inputs for g++, e.g "g++ Metpreproc.cpp emailparser.cpp -o meto". Of course, I'm just guessing on the name of the source file. And the complete project may need many other source files to be complete - I can't know that.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Aug 2008
    Ah great! I am actually doing a makefile, just so I learn it too - Java has none of that. C++ is interesting, and from what I read trying to work that first error, it is trully more portable than Java (there are some issues with it, encodings and stuff).
    Hopefully one day i'll have a good reason to work on something with C++

    This small program has 5 files: the main I wrote (renamed from Metpreproc.cpp to main.cpp - its shorter), base64.h + base64.cpp, emailParser.h + emailParser.cpp. Not that many files, but still going for a makefile. Hopefully will not add up to the error messages :s

    Thank you all

  7. #7
    Registered User
    Join Date
    Aug 2008
    Awesome, the makefile worked, no errors.
    Now i'll just go through the stringstream documentation to figure out how to get the info out of it.
    Thank you all once again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Compiling Problem
    By Deronius in forum C++ Programming
    Replies: 3
    Last Post: 06-15-2008, 11:23 AM
  2. Problem compiling files that store functions
    By tiachopvutru in forum C++ Programming
    Replies: 10
    Last Post: 05-30-2008, 05:42 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Compiling in Unix vs Visual C++
    By stimpyzu in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2002, 06:41 AM
  5. Compiling syntax
    By Jez_Master in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2002, 09:46 PM