Java to C++

Started by
15 comments, last by rip-off 15 years, 5 months ago
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.
Advertisement
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 << " stemmen voor " << missen.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..
};
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.
about the string issue ... is it possible to use #include <cstring>?
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.
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?
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 using 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.

This topic is closed to new replies.

Advertisement