Compiling

This is a discussion on Compiling within the C++ Programming forums, part of the General Programming Boards category; Hello! I was sent some C++ code that will be used to process files, by creating an object that takes ...

  1. #1
    laz
    laz is offline
    Registered User
    Join Date
    Aug 2008
    Posts
    4

    Question Compiling

    Hello!
    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:
    Code:
    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:

    Code:
    #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
    Thanks!

    =edit=
    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
    Location
    Singapore
    Posts
    21,327
    Are you a Java programmer, by any chance? The first most obvious problem is:
    Code:
    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:
    Code:
    stringstream ftw;
    Also, instead of casting argv[1] to string, you would likely want to just initialise myfile with argv[1], e.g.,
    Code:
    string myfile(argv[1]);
    Of course, you should check that argc is greater than 1 before using argv[1].
    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

  3. #3
    laz
    laz is offline
    Registered User
    Join Date
    Aug 2008
    Posts
    4
    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...
    Code:
    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
    Posts
    14,185
    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
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

    --
    Mats
    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
    laz
    laz is offline
    Registered User
    Join Date
    Aug 2008
    Posts
    4
    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
    laz
    laz is offline
    Registered User
    Join Date
    Aug 2008
    Posts
    4
    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, 08:46 PM

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