C++ Linear Progression, code won't work - c++11

Please help me to understand what is wrong in this first line of my code it keeps saying expected unqualified id before public.
public class LinearRegressionModel extends RegressionModel {
/* The y intercept of the straight line */
private double a;
/* The gradient of the line */
private double b;
...
}
Error :
[Error] expected unqualified-id before 'public'

Just a minor thing: it's written in the wrong language! C++ would look more like this:
class LinearRegressionModel : public RegressionModel {
private:
double a;
double b;
...
public:
/* public stuff here */
}

Related

How can I access a protected variable in child class through an abstract class pointer?

I have an abstract class that I have derived from two child classes. One of them has a protected variable that the other one does not. To make the code more general, I have to use an smart pointer of the abstract class. Is there any way to access the protected variable through the pointer? As an example, Consider the following code (the real code is huge and I had to write this sample code):
class Pen{
public:
pen(string _color): color(_color){};
getColor(){return color;};
protected:
string color;
};
// base abstract class
class writer{
public:
writer() {}
virtual changeColor(string color) = 0;
};
class oldWriter: public writer{
protected:
Pen *pen;
public:
oldWriter(string _pen):
pen(_pen){}
virtual changeColor(string color){ pen->color = color;};
};
class youngWriter: public writer{
protected:
Pen *pen;
Pencile pencil; //we need to have access to pencil
public:
youngWriter(string _pen):
pen(_pen){}
virtual changeColor(string color){ pen->color = color;};
Pencil getPencil(){return pencil;};
};
int main(){
unique_ptr<Writer> artist;
Pencil pencil = artist->getPencil(); //how?
}
How can we access "pencil" in "youngWriter" class through "artist"?
if i well understood the code, the problem is related to class "Writer" which haven't a method called getPencil.
getPencil is a method of class youngWriter.
Therefore:
artist have to be a youngWriter instance.
Writer have to contains a method getPencil abstract.

In C++11 can the override and final keywords be used only in the declaration and not in the definition?

It seems that override and finalspecifiers can be used in both declaration and definition. Is it possible to only use them at the declaration level ?
The override and final specifiers can only appear in member definitons if the definition is inside a class definition.
E.g.:
struct Base { virtual void foo() = 0 }
struct Derived : public Base { void foo() override { std::cout << "foo"; } // OK
struct Derived : public Base { void foo() override; }
void Derived::foo() override { std::cout << "foo"; } // Error!
// ^^ Definition outside class. ^^
In other words, if you put the definition of a member function outside of the class definition, then yes, the override and final specifiers should only be present in the declaration inside the class definition.
http://en.cppreference.com/w/cpp/language/final

JNA : Error when to call natives functions with pointer to struture as parameters

I tested this int EMV_Init(EMV_PARAMS *params, EMV_HANDLE *hEMV) in C, it works well.
Now i want to call it in my java application by means of jna. This function is in a native library under windows (dll file).
EMV_PARAMS is a structure
typedef struct
{
HAL_UI_HANDLE ui;
HAL_SCR_HANDLE card;
HAL_PROPERTY_HANDLE property;
HAL_DATE_HANDLE date;
HAL_CRYPTO_HANDLE crypto;
HAL_CHV_HANDLE chv;
} EMV_PARAMS;
Note that all attributes in EMV_PARAMS is is an opaque structure like this typedef void * HAL_UI_HANDLE;
EMV_HANDLE is also an opaque structure : typedef void *EMV_HANDLE;
I need your help to fix the following error:
Exception in thread "main" java.lang.NullPointerException at
com.sun.jna.Structure.getFields(Structure.java:895)
at com.sun.jna.Structure.deriveLayout(Structure.java:1042)
at com.sun.jna.Structure.calculateSize(Structure.java:966)
at com.sun.jna.Structure.calculateSize(Structure.java:933)
at com.sun.jna.Structure.allocateMemory(Structure.java:360)
at com.sun.jna.Structure.<init>(Structure.java:184)
at com.sun.jna.Structure.<init>(Structure.java:172)
at com.sun.jna.Structure.<init>(Structure.java:159)
at com.sun.jna.Structure.<init>(Structure.java:151)
I created a Java interface named "CInterface" which contains "EMV_PARAMS" class.
public interface CInterface extends Library
{
CInterface INSTANCE = (CInterface) Native.loadLibrary("path to dll", CInterface.class);
public int EMVCT_Init(EMV_PARAMS.ByReference params, Pointer hEMV);
public static class PARAMS extends Structure
{
public static class ByReference extends PARAMS implements Structure.ByReference {}
Pointer ui;
Pointer card;
Pointer property;
Pointer date;
Pointer crypto;
Pointer chv;
#Override
protected List getFieldOrder() {
// TODO Auto-generated method stub
return null;
}
}
}
java test class
public static void main(String[] args)
{
CInterface.EMV_PARAMS.ByReference emv_param = new
CInterface.EMV_PARAMS.ByReference();
int test= 0;
Pointer hEMV = null;
test=CInterfaceEMV.INSTANCE.EMVCT_Init(emv_param, hEMV);
System.out.println("test="+test);
}
Thank you for your attention
Your structure fields must be public, and you need to implement getFieldOrder().

In Java compiler,The print in System.out.print can be defined as identifier or Keyword? [duplicate]

This question already has answers here:
In Java compiler,which type can be defined as identifier ( ID ) or Keyword (reserved word)?
(2 answers)
Closed 7 years ago.
I have studied about java which have listed 50 Java keywords. There is a homework of Lex, the goal is to recognize the word is keywords, IDs, symbols, operators. But there is one more little problem is the code below, is print in System.out.print() an ID or keyword?
public class HelloWorld {
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int c;
int a = 5;
c = add(a, 10);
if (c > 10)
System.out.print("c = " + -c);
else
System.out.print(c);
System.out.print("Hello World");
}
}
print is the name of a method in the java.io.PrintStream class, hence an ID. Keywords are those which generally turn blue or another colour when you type them in most IDEs.
For more information: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/index.html
System is a final class from java.lang package.
out is the reference of PrintStream class and a static member of System class.
print is a method of PrintStream class.
//the System class belongs to java.lang package
class System {
public static final PrintStream out;
//...
}
//the Prinstream class belongs to java.io package
class PrintStream{
public void print();
//...
}
Have a look at this too.. https://docs.oracle.com/javase/7/docs/api/java/lang/System.html

Casting an object with events defined to an interface type causes an internal compiler error

I've got an interface with a simple signature:
namespace Serial {
public interface struct ISerial
{
uint16_t func1();
uint16_t func2();
};
}
and then a class type which implements the interface
namespace Serial {
public delegate void MyEventClass();
public ref class MySerial sealed : public ISerial {
public:
event MyEventClass MyEvent;
MySerial();
...
};
}
but elsewhere, as a default parameter to a function, I try to store a reference to a type MySerial as an ISerial ^
void
begin(
Serial::ISerial ^s = ref new Serial::MySerial
);
causes: error C1001: An internal error has occurred in the compiler.
when I remove the event from the class definition, everything compiles fine. I'm finding little information on this error.
I verified this on VS 2013 and it works with a few minor changes (all generated based on normal compiler errors, not an ICE). I don't have VS 2015 available right now, but will log a bug if it still repros.
First the struct (should be unchanged)
namespace Serial
{
public interface struct ISerial
{
uint16_t func1();
uint16_t func2();
};
}
Then the class (couple of changes noted below):
namespace Serial
{
public delegate void MyEventClass();
public ref class MySerial sealed : public ISerial{
public:
event MyEventClass^ MyEvent;
MySerial(){}
virtual uint16_t func1() { return 42; }
virtual uint16_t func2() { return 42; }
};
}
And the usage:
void foo()
{
using namespace Serial;
ISerial^ foo = ref new MySerial();
}
Basically you need to add the hat (^) to the event type, and you need to add virtual to the methods (but do not add override).
See more here on MSDN

Resources