Getting the following error on points function - c++

Hi I am getting the following error when i am trying to compile my code.
error C2143: syntax error : missing ';' before '.' when i call
Points.addPoints();
Thanks for any help.
#ifndef _POINTS_H
#define _POINTS_H
//points.h
#include <sstream>
using namespace std;
class Points{
int pointsADD;
int pointsRemove;
int newPoints;
public :
Points(int points){this->pointsADD=points;this->pointsRemove=pointsRemove;this->newPoints=newPoints;}
void addPoints(int newPointsADD){
newPoints=pointsADD+newPointsADD++;
}
void removePoints(int newPointsRemove){
newPoints=pointsRemove+newPointsRemove--;
}
int getPoints(){
return newPoints;
}
};
#endif

You need an instance of the class to add anything. Read a good C++ introduction, and fix for now:
Points p(42);
p.addPoints(23);

Related

How to pass an enum class which is inside another class's function by reference?

I am still fairly new to C++, so sorry if the code is a bit amateurish, that might be the source of the problem.
I've spent some time searching this site and across the web. There are plenty of examples using enums (not enum class) and also of using enums and enums class outside of classes, but didn't really find anything useful for my particular scenario, see below. Of course, I may have seen the answer, but my C++ is not advanced enough yet to recognize it.
Basically I want to pass 2 "state" enums classes into a method, in a different class from the ones in which the enums classes are defined, and then change the state of the second enum class based on the value in the first enum class. These enum classes are defined inside a class which contains a pointer to the second class in which the method is defined. Class declarations are in header files and defined in separate .cpp files which include the relevant headers. See the detailed code and errors below.
Hoping someone can help me interpret the error messages at the end and figure out how to achieve this.
main.cpp
#include <iostream>
#include "firstfile.h"
int main() {
FirstFile firstobject;
firstobject.Function1();
}
Basically, I've got 2 enum classes inside following class.
firstfile.h
#include "secondfile.h"
class FirstFile
{
protected:
SecondFile secondobject;
public:
enum class enum1 {
Option1,
Option2
} enumIn = enum1::Option1;
enum class enum2 {
Option3,
Option4
} enumInOut = enum2::Option3;
// Methods
protected:
public:
void Function1();
};
firstfile.cpp
#include <iostream>
#include "firstfile.h"
void FirstFile::Function1()
{
std::cout << "Before the function call enumIn = Option 1 and enumInOut = Option3 " << std::endl;
secondobject.function2(enumIn, enumInOut);
if (enumInOut == enum2::Option4) {
std::cout << "After the function call enumInOut = Option 4" << std::endl;
}
else if (enumInOut == enum2::Option3) {
std::cout << "After the function call enumInOut = Option 3" << std::endl;
}
else {
std::cout << "enumInOut didn't match either Option 3 or Option 4" << std::endl;
}
}
The header file, where most of the errors occur.
secondfile.h
#include "firstfile.h"
class SecondFile
{
public:
void function2(const enum1& enumIn, enum2& enumInOut);
};
Finally, here is the method in class2 that is being called that should update enum2 based on the value of enum1.
secondfile.cpp
#include <iostream>
#include "firstfile.h"
#include "secondfile.h"
void SecondFile::function2(const enum1& enumIn, enum2& enumInOut)
{
if (enumIn == FirstFile::enum1::Option1) {
enumInOut = FirstFile::enum2::Option4;
}
}
The errors are:
firstfile.cpp
\secondfile.h(16, 28) : error C4430 : missing type specifier - int assumed.Note : C++ does not support default - int
\secondfile.h(16, 28) : error C2143 : syntax error : missing ',' before '&'
\firstfile.cpp(8, 42) : error C2660 : 'SecondFile::function2' : function does not take 2 arguments
\secondfile.h(16, 7) : message: see declaration of 'SecondFile::function2'
Main.cpp
\secondfile.h(16, 28) : error C4430 : missing type specifier - int assumed.Note : C++ does not support default - int
\secondfile.h(16, 28) : error C2143 : syntax error : missing ',' before '&'
secondfile.cpp
\secondfile.h(16, 28) : error C4430 : missing type specifier - int assumed.Note : C++ does not support default - int
\secondfile.h(16, 28) : error C2143 : syntax error : missing ',' before '&'
\secondfile.cpp(5, 39) : error C4430 : missing type specifier - int assumed.Note : C++ does not support default - int
\secondfile.cpp(5, 39) : error C2143 : syntax error : missing ',' before '&'
\secondfile.cpp(6, 6) : error C2065 : 'enumIn' : undeclared identifier
\secondfile.cpp(7, 3) : error C2065 : 'enumInOut' : undeclared identifier
Hope this all makes sense and look forward to any insights to help understand the cause of the errors and how I might be able to resolve them.
It's probably related to scope, but hoping to learn from this experience.
You have two issues:
You have circular dependencies, since you add the header of each classes to each other's header files. You can solve it via pointers members. Make the class member secondobject in the class FirstFile as a pointer, and provide a forward declaration in the header (i.e. in firstfile.h) for SecondFile.
Secondly, you need to specify the enum1 and enum2 from which class/ scope it is. You can use the using specifier for this, and enums will be available for the entire class SecondFile's scope.
That means, you need something like: (See Online)
firstfile.h
class SecondFile; // forward declaration
class FirstFile
{
protected:
SecondFile* secondobject{ nullptr }; // or smart pointers
public:
// ...enums and functions
};
firstfile.cpp
#include <iostream>
#include "secondfile.h"
void FirstFile::Function1()
{
// ...other code
if(secondobject)
secondobject->function2(enumIn, enumInOut);
// ^^^^^^^^^^^^
}
secondfile.h
#include "firstfile.h"
class SecondFile
{
public:
using enum1 = FirstFile::enum1; // specify from where the enums are
using enum2 = FirstFile::enum2;
void function2(const enum1& enumIn, enum2& enumInOut);
// ...other codes
};
secondfile.cpp
#include "secondfile.h"
void SecondFile::function2(const enum1& enumIn, enum2& enumInOut)
{
if (enumIn == enum1::Option1) {
enumInOut = enum2::Option4;
}
}

C++ error C2143 syntax error : missing ';' before function name

I have header file :
#ifndef VIP_TICKET_H
#define VIP_TICKET_H
#include "ticket.h"
class VIPTicket : public Ticket
{
public:
enum VIPType { FIRST_CLASS, FAST_LINE };
VIPType getTicketType() const;
private:
VIPType type;
};
#endif
and it's cpp file
#include "vipTicket.h"
VIPType VIPTicket::getTicketType() const
{
return type;
}
the error says " error C2143: syntax error : missing ';' before 'VIPTicket::getTicketType' "
this error is very confusing.. i guess it's not a ';' that is missing but probably something else wrong with the code that I can't put my finger on..
The problem is this definition
VIPType VIPTicket::getTicketType() const
{
...
}
When you define this function you have to remember that VIPType is not in the global scope, but in the scope of the VIPTicket class, so you have to explicitly mention the scope:
VIPTicket::VIPType VIPTicket::getTicketType() const
{
...
}

ERROR C2143: Syntax error: missing ';' before '<' C++

I am having trouble in my C++ code where I have to make a binary heap. It works fine as long as I had the "main" function inside of my "MyHeap.h" file but my professor wants it to run in a separate test file. For some reason the code doesn't want to run whenever I try to put the main function outside of the "MyHeap.h" file. When it runs I get the following error:
error C2143: syntax error: missing';' before '<'
I looked at my code and this is where it says there is an error but I can't see anything.
// MyHeap.h
#ifndef _MYHEAP_H
#define _MYHEAP_H
#include <vector>
#include <iterator>
#include <iostream>
class Heap {
public:
Heap();
~Heap();
void insert(int element);
int deletemax();
void print();
int size() { return heap.size(); }
private:
int left(int parent);
int right(int parent);
int parent(int child);
void heapifyup(int index);
void heapifydown(int index);
private:
vector<int> heap;
};
#endif // _MYHEAP_H
So like I said whenever I have the the int main function right after the private class, it will work just fine. Now when I implement it into my test file which is this:
#include "MyHeap.h"
#include <vector>
#include <iostream>
int main()
{
// Create the heap
Heap* myheap = new Heap();
myheap->insert(25);
myheap->print();
myheap->insert(75);
myheap->print();
myheap->insert(100);
myheap->print();
myheap->deletemax();
myheap->print();
myheap->insert(500);
myheap->print();
return 0;
}
It keeps popping up the errors, any ideas on I could go about fixing this problem so that my code can run from a test file?
Use std::vector instead of vector.
The compiler is complaining it doesn't know about vector.
Since it lives in std namespace, the safest solution is to prefix with std.

Can some one have a look at my code? error C2059: syntax error : 'public'

I am writing an algebra tree program. While compiling, I got a lot of errors. I do not know where the errors come from.
Here is my code:
//file: Term.h
#ifndef TERM
#define TERM
#include <sstream>
#include <string>
using namespace std;
class Term {
public:
Term() {}
virtual ~Term() {}
virtual string symbolicEval() = 0;
virtual double numericalEval(double X) = 0;
};
#endif
//file: UnaryOp.h
#ifndef UNARYOP
#define UNARYOP
#include "Term.h";
class UnaryOp: public Term{
protected:
Term* Child;
public:
UnaryOp(Term* l){Child = l;};
virtual ~UnaryOp(){delete Child;};
virtual string symbolicEval(){};
virtual double numericalEval(){};
};
#endif UNARYOP
//file:CCos.h
#ifndef COS_H
#define COS_H
#include "UnaryOp.h"
class Cos: public UnaryOp{
public:
Cos(Term * l):UnaryOp(l){};
virtual ~ Cos(){};
virtual string symbolicEval(){
ostringstream oss;
oss << "cos(x)" << endl;
return oss.str();
};
virtual double numericalEval(double X){
return cos(Child->numericalEval(X));
}
}
#endif COS_H
While compiling, I got the following errors:
1>c:\users\administrator\desktop\algebra\algebra\unaryop.h(3): warning C4067: unexpected tokens following preprocessor directive - expected a newline
1>c:\users\administrator\desktop\algebra\algebra\ccos.h(6): error C2236: unexpected 'class' 'Cos'. Did you forget a ';'?
1>c:\users\administrator\desktop\algebra\algebra\ccos.h(6): error C2143: syntax error : missing ';' before ':'
1>c:\users\administrator\desktop\algebra\algebra\ccos.h(6): error C2059: syntax error : ':'
1>c:\users\administrator\desktop\algebra\algebra\ccos.h(6): error C2059: syntax error : 'public'
1>c:\users\administrator\desktop\algebra\algebra\ccos.h(6): error C2143: syntax error : missing ';' before '{'
1>c:\users\administrator\desktop\algebra\algebra\ccos.h(6): error C2447: '{' : missing function header (old-style formal list?)
1>c:\users\administrator\desktop\algebra\algebra\algebra.cpp(29): error C2061: syntax error : identifier 'Cos'
Can someone give me a hint where am I wrong?
Start with replacing
#include "Term.h";
by
#include "Term.h"
(there may be more things wrong). And actually, the first error message exactly told you that the preprocessor was expecting a newline where you wrote a semicolon, so next time please read the error messages first.
Multiple things.
Why are you providing the definition for methods in header files?
//file:CCos.h
#ifndef COS_H
#define COS_H
#include "UnaryOp.h"
class Cos: public UnaryOp{
public:
Cos(Term * l):UnaryOp(l){};
virtual ~ Cos(){};
virtual string symbolicEval(){
ostringstream oss;
oss << "cos(x)" << endl;
return oss.str();
};
virtual double numericalEval(double X){
return cos(Child->numericalEval(X));
}
}
#endif COS_H
If you are using the "{}" after a method declaration in a ".H" file, would you please explain what does that mean?
How does a compiler understands it?
When do you use a colon? Is it after a method declaration or after a definition or for bothboth?
Are .h files compiled? why? why not?
If you could answer these, you have your answer. It is more of a homework. Request you to please post only relevant & only those questions, for which you need technical assistance.
you are missing closing brace of virtual string symbolicEval() function.

undeclared identifier

Need a second set of eyes. I am getting the following error:
1>c:\users\thom\documents\cworkspace\barnaby\barnaby\timezone.h(15): error C2065: 'TransitionTimeInfo' : undeclared identifier
Here is the line of code on which I'm receiving the error:
Timezone(std::vector<LeapSecondsInfo> &leapSecondsVector, std::vector<unsigned char> &localTimeTypes, std::vector<P6::UINT8> &stdWallIndicators, &std::vector<unsigned long> &transitionTimes, std::vector<TransitionTimeInfo> &transitionTimesInfo, std::vector<P6::UINT8> &utcLocalIndicators){
This is the line for the constructor for my class. This file has the following include:
#include "stdafx.h"
And here is the salient part of the stdafx.h:
#include "targetver.h"
#include "barnaby.h"
#include "LeapSecondsInfo.h"
#include "p6types.h"
#include "Timezone.h"
#include "TransitionTimeInfo.h"
And here is TransitionTimeInfo.h:
class TransitionTimeInfo
{
public:
TransitionTimeInfo(long gmtOffset, bool daylightSavings, unsigned int abbreviationIndex){
setAbbreviationIndex(abbreviationIndex);
setDaylightSavings(daylightSavings);
setGmtOffset(gmtOffset);
}
virtual ~TransitionTimeInfo(void) {};
unsigned int getAbbreviationIndex(){
return abbreviationIndex;
}
void setAbbreviationIndex(unsigned int newVal){
abbreviationIndex = newVal;
}
bool isDaylightSavings(){
return daylightSavings;
}
void setDaylightSavings(bool newVal){
daylightSavings = newVal;
}
long getGmtOffset(){
return gmtOffset;
}
void setGmtOffset(long newVal){
gmtOffset = newVal;
}
private:
long gmtOffset;
bool daylightSavings;
unsigned int abbreviationIndex;
};
What's more, if I click on the type name and hit F12 (Visual C++) it takes me to this file.
Any ideas?
Thanks.
Change the order of includes:
#include "TransitionTimeInfo.h"
#include "Timezone.h"
The Timezone.h uses TransitionTimeInfo but the "TransitionTimeInfo.h" is included after it.
Ideally, You should always follow the rule:
Each file should include all the header files it needs and not rely on them getting included indirectly through some other files.
So, You should include "TransitionTimeInfo.h" in "Timezone.h".