Menu

From java to C++

Lektroluv
2008-11-15
2012-09-26
  • Lektroluv

    Lektroluv - 2008-11-15

    I've have made a project in java.
    With some adjustments, it should be possible to make this program work in c++.
    I've implemented most of the things.
    But now I'm a little bit stuck.
    This is my log:

    Compiler: Default compiler
    Building Makefile: "C:...\Makefile.win"
    Bezig met uitvoeren van make...
    make.exe -f "C:...\Makefile.win" all
    g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"

    In file included from Jury.h:1,
    from main.cpp:5:
    Kandidate.h:7: error: redefinition of class Kandidate' Kandidate.h:7: error: previous definition ofclass Kandidate'

    main.cpp: In function `int main(int, char**)':

    main.cpp:12: error: invalid conversion from const char*' tochar'
    main.cpp:12: error: initializing argument 1 of Kandidate::Kandidate(char, int)' main.cpp:13: error: invalid conversion fromconst char' to char' main.cpp:13: error: initializing argument 1 ofKandidate::Kandidate(char, int)'
    main.cpp:14: error: invalid conversion from const char*' tochar'
    main.cpp:14: error: initializing argument 1 of Kandidate::Kandidate(char, int)' main.cpp:15: error: invalid conversion fromconst char
    ' to `char'

    main.cpp:15: error: initializing argument 1 of Kandidate::Kandidate(char, int)' main.cpp:16: error: invalid conversion fromconst char' to char' main.cpp:16: error: initializing argument 1 ofKandidate::Kandidate(char, int)'
    main.cpp:17: error: invalid conversion from const char*' tochar'
    main.cpp:17: error: initializing argument 1 of Kandidate::Kandidate(char, int)' main.cpp:18: error: invalid conversion fromconst char
    ' to char' main.cpp:18: error: initializing argument 1 ofKandidate::Kandidate(char, int)'
    main.cpp:19: error: invalid conversion from const char*' tochar'
    main.cpp:19: error: initializing argument 1 of Kandidate::Kandidate(char, int)' main.cpp:20: error: invalid conversion fromconst char' to char' main.cpp:20: error: initializing argument 1 ofKandidate::Kandidate(char, int)'
    main.cpp:21: error: invalid conversion from const char*' tochar'
    main.cpp:21: error: initializing argument 1 of Kandidate::Kandidate(char, int)' main.cpp:22: error: invalid conversion fromconst char
    ' to char' main.cpp:22: error: initializing argument 1 ofKandidate::Kandidate(char, int)'
    main.cpp:23: error: invalid conversion from const char*' tochar'
    main.cpp:23: error: initializing argument 1 of `Kandidate::Kandidate(char, int)'

    make.exe: *** [main.o] Error 1

    Excecution complete

    This is a part of main.cpp:

    int main(int argc, char *argv[]) {
    Kandidate miss1("Emma", 9901);
    Kandidate miss2("Veerle", 9902);
    Kandidate miss3("Laura", 9903);
    Kandidate miss4("Sandra", 9904);
    ......

    This is a part of Kandidate.cpp:

    include "Kandidate.h"

    include <iostream>

    using namespace std;

    Kandidate::Kandidate(char naam, int nummer) {
        this-&gt;naam = naam;
        this-&gt;nummer = nummer;
        cout &lt;&lt; &quot;Miss &quot; &lt;&lt; naam &lt;&lt; &quot;\tmet nummer\t&quot; &lt;&lt; nummer &lt;&lt; endl;
    }
    
     
    • cpns

      cpns - 2008-11-16

      Additionally, with C style strings, you probably cannot do this:

      this->naam = naam;

      That at best would merely copy a pointer. It may work in this limited case because you originally used a literal constant, whose address may persist, but it does not copy the string, so if the original string were a temporary variable, you'd be in trouble.

      To copy C strings you need to use the strcpy() function. However for that to work, this->naam must have space allocated to it, so must either be an array or a pointer to some otherwise allocated space - not an uninitialised pointer.

      As I suggested, this is all much easier if you were to use std::string.

      Clifford

       
    • cpns

      cpns - 2008-11-16

      The errors are reported for line 7 of Kandidate.h and 12-23 of main.cpp. The least you could do is post all teh code referenced in the error messages.

      With respect to Kandidate.h(7), the most likely guess is lack of include guards ( http://en.wikipedia.org/wiki/Include_guard )

      With respect to main.cpp(12) and all the others, did you actually read the error message? Is is exactly what it says, you have passed a const char* argument to a function that expects a char argument.

      If the char argument concerned was intended to be a C string, then it should be char*. A char is simply a small integer, you need an array of char to hols a C string. Now that said, this is C++ and you no longer need ot use C's char array kludge to perform string handling, C++ has a std::string class, that as a Java programmer you will almost certainly be more comfortable with.

      A hint when reading GCC error messages, the compiler often uses more than one line for what is effectively a single message; so for example:

      > main.cpp:12: error: invalid conversion from const char*' tochar'
      > main.cpp:12: error: initializing argument 1 of `Kandidate::Kandidate(char, int)'

      is just one error message, and needs to be read together. Like "invalid conversion from const char*' tochar' when initializing argument 1 of `Kandidate::Kandidate(char, int)' ".

      You should find that the messages make far more sense when you read them like that. Often teh two lines reference separate code lines - usually the usage location and the associated definition or declaration location.

      Clifford.

       
    • cpns

      cpns - 2008-11-16

      > The least you could do is post all the code
      > referenced in the error messages.

      I meant in such a way that we may match the lines with the errors. Upon reflection I am assuming that

      Kandidate miss1("Emma", 9901);

      is line 12? But you still did not post any part of kandidate.h

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.