Java to C++
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?
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:
Concepts you might not be familiar with yet:
* References.
* Initialiser lists.
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.
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).
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.
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();
};
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();
};
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?
Are you using include guards?
#ifndef KANDIDATE_H#define KANDIDATE_H#include <string>class Kandidate{ // ...};#endif
You had classes and header files, but not definitions and include guards?
Anyway, where do you include Kandidate.h?
Anyway, where do you include Kandidate.h?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement