Sign in to follow this  

Java to C++

This topic is 3313 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have to implement a java project in C++, which should be almost the same. This is the main of a project I have made that works in Java: public class VerkiezingMain { public static void main(String[] args) { System.out.println("Welkom: "); Kandidate miss1 = new Kandidate("Emma", 9901); Kandidate miss2 = new Kandidate("Veerle", 9902); Kandidate miss3 = new Kandidate("Laura", 9903); Kandidate miss4 = new Kandidate("Sandra", 9904); Kandidate miss5 = new Kandidate("Els", 9905); Kandidate miss6 = new Kandidate("Jolien", 9906); Kandidate miss7 = new Kandidate("Anne", 9907); Kandidate miss8 = new Kandidate("Kelly", 9908); Kandidate miss9 = new Kandidate("Veronique", 9909); Kandidate miss10 = new Kandidate("Lindsay", 9910); Kandidate miss11 = new Kandidate("Joke", 9911); Kandidate miss12 = new Kandidate("Sarah", 9912); Kandidate[] alleKandidates = {miss1, miss2, miss3, miss4, miss5, miss6, miss7, miss8, miss9, miss10, miss11, miss12}; int[] expertPunten = {15, 12, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; Verkiezing bb = new Verkiezing(alleKandidates); Televoting tele = new Televoting(alleKandidates); Jury expert = new Jury(alleKandidates,expertPunten); tele.stemOpMiss(9909); // mag niet uitgevoerd worden want televoting is gesloten tele.openTele(); tele.stemOpMiss(9901); tele.stemOpMiss(9909); tele.stemOpMiss(9909); tele.stemOpMiss(9902); tele.stemOpMiss(9905); tele.stemOpMiss(9904); tele.stemOpMiss(9907); tele.stemOpMiss(9908); tele.stemOpMiss(9909); tele.stemOpMiss(9901); tele.stemOpMiss(9910); tele.stemOpMiss(9912); tele.stemOpMiss(9903); tele.stemOpMiss(9901); tele.stemOpMiss(9909); tele.stemOpMiss(9909); tele.stemOpMiss(9906); tele.stemOpMiss(9901); tele.stemOpMiss(9904); tele.stemOpMiss(9907); tele.stemOpMiss(9912); tele.stemOpMiss(9908); tele.stemOpMiss(9909); tele.stemOpMiss(9910); tele.stemOpMiss(9912); tele.stemOpMiss(9911); tele.stemOpMiss(9903); tele.stemOpMiss(9909); tele.openTele(); tele.geefUitslagTele(); expert.geefExPunten(9909, 15); expert.geefExPunten(9902, 12); expert.geefExPunten(9903, 10); expert.geefExPunten(9910, 9); expert.geefExPunten(9905, 8); expert.geefExPunten(9906, 7); expert.geefExPunten(9907, 6); expert.geefExPunten(9908, 5); expert.geefExPunten(9911, 4); expert.geefExPunten(9901, 3); expert.geefExPunten(9904, 2); expert.geefExPunten(9912, 1); bb.berekenKlassement(tele.geefArrayStemmen(), expert.geefArrayExPunten()); bb.sorteer(); bb.geefWinnaar(); } } Please don't comment on the main in java, I don't want to change it. This is what I have in C++: #include <cstdlib> #include <iostream> #include "Kandidate.h" #include "Televoting.h" #include "Jury.h" #include "Verkiezing.h" using namespace std; int main(int argc, char *argv[]) { cout << "Welkom, je kan op volgende kandidates stemmen als de televoting geopend is: " << endl; Kandidate miss1("Emma", 9901); Kandidate miss2("Veerle", 9902); Kandidate miss3("Laura", 9903); Kandidate miss4("Sandra", 9904); Kandidate miss5("Els", 9905); Kandidate miss6("Jolien", 9906); Kandidate miss7("Anne", 9907); Kandidate miss8("Kelly", 9908); Kandidate miss9("Veronique", 9909); Kandidate miss10("Lindsay", 9910); Kandidate miss11("Joke", 9911); Kandidate miss12("Sarah", 9912); Kandidate alleKandidates[12] = {miss1, miss2, miss3, miss4, miss5, miss6, miss7, miss8, miss9, miss10, miss11, miss12}; int expertPunten[12] = {15, 12, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; Verkiezing bb(alleKandidates); Televoting tele(alleKandidates); Jury expert(alleKandidates, expertPunten); tele.stemOpMiss(9909); // mag niet uitgevoerd worden want televoting is gesloten tele.openTele(); tele.stemOpMiss(9901); tele.stemOpMiss(9909); tele.stemOpMiss(9909); tele.stemOpMiss(9902); tele.stemOpMiss(9905); tele.stemOpMiss(9904); tele.stemOpMiss(9907); tele.stemOpMiss(9908); tele.stemOpMiss(9909); tele.stemOpMiss(9901); tele.stemOpMiss(9910); tele.stemOpMiss(9912); tele.stemOpMiss(9903); tele.stemOpMiss(9901); tele.stemOpMiss(9909); tele.stemOpMiss(9909); tele.stemOpMiss(9906); tele.stemOpMiss(9901); tele.stemOpMiss(9904); tele.stemOpMiss(9907); tele.stemOpMiss(9912); tele.stemOpMiss(9908); tele.stemOpMiss(9909); tele.stemOpMiss(9910); tele.stemOpMiss(9912); tele.stemOpMiss(9911); tele.stemOpMiss(9903); tele.stemOpMiss(9909); tele.openTele(); tele.geefUitslagTele(); expert.geefExPunten(9909, 15); expert.geefExPunten(9902, 12); expert.geefExPunten(9903, 10); expert.geefExPunten(9910, 9); expert.geefExPunten(9905, 8); expert.geefExPunten(9906, 7); expert.geefExPunten(9907, 6); expert.geefExPunten(9908, 5); expert.geefExPunten(9911, 4); expert.geefExPunten(9901, 3); expert.geefExPunten(9904, 2); expert.geefExPunten(9912, 1); bb.berekenKlassement(tele.geefArrayStemmen(), expert.geefArrayExPunten()); bb.sorteer(); bb.geefWinnaar(); system("PAUSE"); return EXIT_SUCCESS; } When I compile this in C++, I get some errors. main.cpp:12: error: invalid conversion from `const char*' to `char' main.cpp:12: error: initializing argument 1 of `Kandidate::Kandidate(char, int)' main.cpp:13: error: invalid conversion from `const char*' to `char' main.cpp:13: error: initializing argument 1 of `Kandidate::Kandidate(char, int)' ... Someone who knows what to do with these errors?

Share this post


Link to post
Share on other sites
Your Kandidate type has a contructor that takes a char as a parameter. A char can only hold a single letter. You want to use a string instead. In C++, we use std::string, available from the <string> include (note that this is not <string.h>, C++ standard library headers lack extensions).

So:

#include <string>

class Kandidate
{
public:
Kandidate(const std::string &name, int attribute)
:
name(name)
attribute(attribute)
{
}

// ...

private:
std::string name;
int attribute;
};


Concepts you might not be familiar with yet:
* References.
* Initialiser lists.

Share this post


Link to post
Share on other sites
Quote:
Original post by Lektroluv
When I compile this in C++, I get some errors.

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

Someone who knows what to do with these errors?

The errors tell you exactly what is wrong. You are trying to convert a const char* to a char, which is illegal. The problem is that the constructor for Kandidate takes a char, while I suspect that what you want is for it to take a std::string (you need to include the string header for that).

Share this post


Link to post
Share on other sites
Quote:
Original post by Lektroluv
I have to implement a java project in C++, which should be almost the same.


Be careful, this may be the road to pain :)

Except for the curly braces and a few shared keywords, Java and C++ are very different.

- compilation model
- distribution model
- exception handling
- resource management
- generics
- copy and assignment semantics

Quote:

Please don't comment on the main in java, I don't want to change it.
This is what I have in C++:

8< -- snip --

When I compile this in C++, I get some errors.

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

Someone who knows what to do with these errors?


Your compiler is telling you that the Kandidate constructor takes a char and you're giving it a const char *. I say that's a pretty big hint to look at the constructor of Kandidate. It's probably defined incorrectly.

Furthermore, prefer std::string to dealing with char arrays directly.

Share this post


Link to post
Share on other sites
Thank you, this solved allot of errors.
I only have these two now:
Kandidate.h:8: error: redefinition of `class Kandidate'
Kandidate.h:8: error: previous definition of `class Kandidate'

class Kandidate {

protected: char naam;
int nummer;
int lengte;
int gewicht;
char haarkleur;
char website;
public:

Kandidate(const std::string &naam, int nummer);

void geefEigenschappen(int lengte, int gewicht, char haarkleur, char website);

char getNaam();

int getNummer();
};

Share this post


Link to post
Share on other sites
Don't forget that the member variable of the Kandidate class will need to be a std::string instance too.

Are you using include guards?

#ifndef KANDIDATE_H
#define KANDIDATE_H

#include <string>

class Kandidate
{
// ...
};

#endif

Share this post


Link to post
Share on other sites
In main.cpp I see

#include "Kandidate.h"
#include "Televoting.h"
#include "Jury.h"
#include "Verkiezing.h"

If Televoting.h or Jury.h or Verkiezing.h also includes Kandidate.h, Kandidate.h will be included more than once in main.cpp which is a big no-no if there are definitions in the header file. You must use include guards to solve that problem:

#ifndef SOME_FUNKY_UNIQUE_IDENTIFIER_USUALLY_DERIVED_FROM_THE_FILE_NAME
#define SOME_FUNKY_UNIQUE_IDENTIFIER_USUALLY_DERIVED_FROM_THE_FILE_NAME

// normal content

#endif

Oh, and you want to read this of course.

Share this post


Link to post
Share on other sites
Yep, I see.
Removed them in other cpp classes and now I get other errors...
Solved most of them myself but these I don't understand. (not that skilled to solve these)

Televoting.h:15: error: `Kandidate' does not name a type
Televoting.h:22: error: expected `)' before "alleKandidates"
Televoting.cpp:8: error: expected `)' before "alleKandidates"
Televoting.cpp:8: error: expected `,' or `;' before "alleKandidates"
Televoting.cpp: In member function `void Televoting::stemOpMiss(int)':
Televoting.cpp:45: error: `missen' undeclared (first use this function)
Televoting.cpp:45: error: (Each undeclared identifier is reported only once for each function it appears in.)
Televoting.cpp: In member function `void Televoting::geefUitslagTele()':
Televoting.cpp:92: error: request for member `length' in `((Televoting*)this)->Televoting::stemmen', which is of non-class type `int[12]'
Televoting.cpp:93: error: `missen' undeclared (first use this function)

Televoting.cpp:

#include "Televoting.h"
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

Televoting::Televoting(Kandidate alleKandidates[12]) {
missen = alleKandidates;
telOpen = 0;
}

void Televoting::openTele() {
std::string tekst1 = "Televoting geopend";
std::string tekst2 = "Televoting gesloten";
if (telOpen == 0) {
for (int i=0; i<tekst1.length()+4; i++) {
cout << "-";
}
cout << endl;
cout << "| " << tekst1 << " |";
for (int i=0; i<tekst1.length()+4; i++) {
cout << "-";
}
cout << endl;
}
telOpen++;
if (telOpen != 1) {
for (int i=0; i<tekst2.length()+4; i++) {
cout << "-";
}
cout << endl;
cout << "| " << tekst2 << " |";
for (int i=0; i<tekst2.length()+4; i++) {
cout << "-";
}
cout << endl;
telOpen = 0;
}
}

void Televoting::stemOpMiss(int stem) {
if (telOpen == 1) {
switch (stem) {
case 9901: cout << "Je hebt op Miss " << missen[0].getNaam() << " gestemd." << endl;
stemmen[0]++;
break;
case 9902: cout << "Je hebt op Miss " << missen[1].getNaam() << " gestemd." << endl;
stemmen[1]++;
break;
case 9903: cout << "Je hebt op Miss " << missen[2].getNaam() << " gestemd." << endl;
stemmen[2]++;
break;
case 9904: cout << "Je hebt op Miss " << missen[3].getNaam() << " gestemd." << endl;
stemmen[3]++;
break;
case 9905: cout << "Je hebt op Miss " << missen[4].getNaam() << " gestemd." << endl;
stemmen[4]++;
break;
case 9906: cout << "Je hebt op Miss " << missen[5].getNaam() << " gestemd." << endl;
stemmen[5]++;
break;
case 9907: cout << "Je hebt op Miss " << missen[6].getNaam() << " gestemd." << endl;
stemmen[6]++;
break;
case 9908: cout << "Je hebt op Miss " << missen[7].getNaam() << " gestemd." << endl;
stemmen[7]++;
break;
case 9909: cout << "Je hebt op Miss " << missen[8].getNaam() << " gestemd." << endl;
stemmen[8]++;
break;
case 9910: cout << "Je hebt op Miss " << missen[9].getNaam() << " gestemd." << endl;
stemmen[9]++;
break;
case 9911: cout << "Je hebt op Miss " << missen[10].getNaam() << " gestemd." << endl;
stemmen[10]++;
break;
case 9912: cout << "Je hebt op Miss " << missen[11].getNaam() << " gestemd." << endl;
stemmen[11]++;
break;
default: cout << "Foutieve stem." << endl;
break;
}
}
else {
cout << "Je kan niet stemmen, de televoting is gesloten." << endl;
}
}

void Televoting::geefUitslagTele() {
cout << "De televoting heeft opgeleverd: " << endl;
for (int i=0; i<stemmen.length; i++) {
cout << stemmen[i] << " stemmen voor " << missen[i].getNaam() << endl;
}
}

int* Televoting::geefArrayStemmen() {
return stemmen;
}

Televoting.h:

#include <cstdlib>
#include <iostream>
#include <string>

class Televoting {
protected: int telOpen;
int stem;
Kandidate missen[12];
int stemmen[12];

public:
Televoting(Kandidate alleKandidates[12]);

void openTele();

void stemOpMiss(int stem);

void geefUitslagTele();

int* geefArrayStemmen(); // is int* right? in java this was int[] geefArr..
};

Share this post


Link to post
Share on other sites
Quote:
Original post by Lektroluv
Yep, I see.
Removed them in other cpp classes

*sigh*

You cannot simply remove the includes from the other cpp files. If they need the headers, they need the headers. You just have to ensure that no cpp file includes the same header file more than once, and the only sane way to do that is to use header guards.

Share this post


Link to post
Share on other sites
Quote:
Original post by Lektroluv
is it possible to use #include <cstring>?

You don't want <cstring> with the C functions strlen&co, you want <string> with the C++ datatype string.

Share this post


Link to post
Share on other sites
Quote:
You don't want <cstring> with the C functions strlen&co, you want <string> with the C++ datatype string.


Suppose I want to use <cstring>
How does that changes things like std::string naam?
Could I use just string naam?

Share this post


Link to post
Share on other sites
Quote:

Suppose I want to use <cstring>



Forget <cstring>, it holds the Standard C Library functions for character array manipulation. By using std::string, you can bypass that. The difference is akin to using java.lang.String instead of manually allocating char [] instances.

Quote:

How does that changes things like std::string naam?
Could I use just string naam?


In a header file, there is nothing you can really do. You can't use the technique I show below because it pollutes the global namespace.

In a source file, you can use a [i]using[i] directive to tell the compiler where to find certain identifiers.

E.g:

// kandidate.h
#ifndef KANDIDATE_H
#define KANDIDATE_H

#include <string>

class Kandidate
{
public:
// fully qualify type names in header files
Kandidate(const std::string &name, int attribute);

// ...

private:
std::string name;
int attribute;
};

#endif

// kandidate.cpp
#include "kandidate.h"

// tell the compiler that "string" means "std::string"
using std::string;

// now we can just use "string".
Kandidate::Kandidate(const string &name, int attribute)
:
name(name)
attribute(attribute)
{
}


However, once you tell the compiler you are using an indentifier (or an entire namespace, by saying using namespace <name>) you cannot undo this operation. This is why you should avoid putting such directives in header files, the client code (the code that includes the headers) have to live with your choices.

Modern languages don't have this problem. In Java, when you import class Bar in Foo.java, other files that happen to import Foo don't import Bar aswell. C++'s compilation model is ancient and a source of some of your confusion.

If you haven't already, I highly recommend you follow the link DevFred gave earlier.

Simple things like knowing how a C++ compiler considers each source file in total isolation will help you to understand what is going on.

Share this post


Link to post
Share on other sites

This topic is 3313 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this