I'm trying to convert a Java program to C++. This is the Java code (which IS working perfectly):
//class MyProgram:
public class MyProgram {
private static ArrayList mylist;
//no constructor
public static int numberOfItems(){
return mylist.size();
}
}
//second Java program, MyList
public class MyList {
public MyList(){
for (int i = 0; i < MyProgram.numberOfItems; i++)
System.out.println("Test");
}
}
This works exactly as I'd hope. However, when converting to C++, the for loop header is giving me errors because it doesn't recognize numberOfItems. The implementation is exactly the same, except a vector instead of an arraylist. My question is: why does not NOT work in C++? Why DOES it work in Java? I've never written an object before where I didn't have to call the constructor to use the methods of that class. How can I achieve this same thing in c++? I can post the c++ code but it looks extremely similar and it seems pointless.
EDIT: C++ CODE:
//MyProgram.h
#ifndef MYPROGRAM_H_
#define MYPROGRAM_H_
#include <vector>
namespace std;
class MyProgram {
public:
int numberOfItems();
private:
vector<Checkpoint> mylist;
};
#endif /* MYPROGRAM_H_ */
//MyProgram.cpp
#include "MyProgram.h"
int MyProgram::numberOfItems()
{
return mylist.size();
}
//MyList.h:
#ifndef MYLIST_H_
#define MYLIST_H_
#include <vector>
using namespace std;
class MyList {
public:
MyList();
virtual ~MyList();
private:
vector<Checkpoint> path;
};
#endif /* MYLIST_H_ */
//mylist.cpp
#include "MyList.h"
MyList::MyList()
{
int i;
for(i = 0; i < MyProgram.numberOfItems(); i++)
{
path.push_back(0);
}
}