Visual C++ doesn't see my class - visual-c++

I was trying to make a basic class program to practice making classes, however even though I followed a tutorial visual ++ says Person() expression must have a class type.
I don't really understand what it's saying and didn't find anything after looking it up. here is my code:
//My main program
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Person.h"
using namespace std;
int main(){
Person person();
cout << person.getName() << "Age = " << person.getAge() << endl;
return 0;
}
//My person.cpp file
#include "stdafx.h"
#include "person.h"
Person::Person() {
name = "unknown";
age = 0;
}
string Person::getName() {
return "person's name is " + name;
}
int Person::getAge() {
return age;
}
//My person.h file
#pragma once
#include "Person.cpp"
#include <iostream>
#include <string>
using namespace std;
class Person {
private:
string name;
int age;
public:
Person();
string getName();
int getAge();
};

To expand on what #Igor Tandetnik is saying when you are creating the instance of Person you are actually declaring a function which is called person and returns a data type of Person. This function takes no parameters, hence: (). What you want to do is
int main(){
Person person;
//...
//...
}
doing it this way creates an instance of a Person called person, or in easier terms creating a Person called bob would be Person bob; and not Person bob();

Related

Rcpp: how to use unwind protection?

I was wondering how could I make some Rcpp code use automatic unwind protection in all Rcpp object creations.
For example, suppose I have some code like this:
#include <stdint.h>
#include <Rcpp.h>
class MyObj {
public:
int val;
MyObj(int val) : val(val) {};
~MyObj() {
std::cout << "I' being destructed - value was: " << val << std::endl;
}
};
// [[Rcpp::export]]
Rcpp::NumericVector crashme(unsigned int seed)
{
srand(seed);
MyObj obj1(rand());
Rcpp::NumericVector out(INT64_MAX-1, 100.);
return out;
}
When I call crashme, obj1 doesn't get destructed before the function ends, due to R's long jumps which I want to protect against.
I see there is a function Rcpp::unwindProtect, but it's implemented as something that takes a callback.
I'm not 100% sure if I'm doing it right, but I managed to add unwind protection like this:
#include <stdint.h>
#include <Rcpp.h>
#include <Rcpp/unwindProtect.h>
// [[Rcpp::plugins(unwindProtect)]]
class MyObj {
public:
int val;
MyObj(int val) : val(val) {};
~MyObj() {
std::cout << "I' being destructed - value was: " << val << std::endl;
}
};
struct NumVecArgs {
size_t size;
double fillwith;
};
SEXP alloc_NumVec(void *data)
{
NumVecArgs *args = (NumVecArgs*)data;
return Rcpp::NumericVector(args->size, args->fillwith);
}
// [[Rcpp::export]]
Rcpp::NumericVector crashme(unsigned int seed)
{
srand(seed);
MyObj obj1(rand());
NumVecArgs args = {INT64_MAX-1, 100.};
Rcpp::NumericVector out = Rcpp::unwindProtect(alloc_NumVec, (void*)&args);
return out;
}
Now calling crashme will successfully destruct obj1 and print the destructor message.
But this is very inconvenient, since I have a series of different Rcpp object allocations taking different constructor types, which would imply either defining a different struct and callback for each one of them, or translating all the calls to lengthy lambda functions.
Is there any way to automatically make all calls to constructors of e.g. Rcpp::NumericVector and Rcpp::IntegerVector have unwind protection?

initializing string in class using constructor

I am creating a class that has two members string and int
I want to use the constructor to initialize both of these two members to use them.
#pragma once
#include <string>
#include <iostream>
using namespace std;
class donation_1
{
public:
//string name;
const char* name;
int donation_amount;
const static size_t string_size = sizeof(string);
const static size_t int_size = sizeof(int);
donation_1(char* name_1 = "Noname", int amount = 0) : name(name_1), donation_amount(amount) {};
};
int main()
{
fstream file;
file.open("donation_total1.txt", ios_base::app);
if (file.is_open())
{
donation_1("xxxx", 20).writedata(file);
donation_1("yyyy", 30).writedata(file);
donation_1("zzzz", 40).writedata(file);
donation_1("MMMM", 50).writedata(file);
donation_1("BBBB", 60).writedata(file);
file.close();
}
else
{
cout << "file couldn't be opened" << endl;
}
return 0;
}
I want to use the constructor to initialize the class variables which I will be using to update a file, however, what I am getting is this error. this error is regarding initializing the string class member.
Severity Code Description Project File Line Suppression State
Error (active) E0310 default argument of type "const char *" is incompatible with parameter of type "char *" Stream_File_Lab D:\INVSPRIVATE\C++\Projects\Stream_File_Lab\donation_1.h 17
The error message is makes it pretty clear. The variable 'name' is declared as const char* but the value being assigned to it is only char* i.e. the const-ness is missing, hence the type incompatibility error throws up.
Please, google for pointer to a const value and how to use them.
Maybe check this tutorial

Error reading characters of string

I had a problem while trying to equal one string to another, I get this error "Error reading characters of string." in Visual Studio 2015, in the line that i am doing this "student_name=otherStudent.student_name;" and I just don't get it.
could you please help me?
Student& Student::operator=(const Student &otherStudent) {
if (this != &otherStudent) // Avoid self assignment
{
student_name=otherStudent.student_name;
for (int i = 0; i < grades_size; i++) {
grades[i] = otherStudent.grades[i];
subjet_names[i] = otherStudent.subjet_names[i];
}
average_grade = otherStudent.average_grade;
}
return *this;
}
Here is the definition of the class if it helps:
#include <iostream>
#include <string>
using namespace std;
#define grades_size 6
class Student {
private:
string student_name;
float grades[grades_size];
string subjet_names[grades_size];
float average_grade;
public:
//Constructor
Student();
Student(string name);
//Print status of Student
void printStudent();
//Operator Overloads
Student & operator=(const Student & otherStudent);
};

error C2064: term does not evaluate to a function taking 0 arguments thread.hpp(60)

I'm creating c++ game server. The server creates many objects monster, and every monster should have its thread with specific function.
I get error :
error C2064: term does not evaluate to a function taking 0 arguments
thread.hpp(60) : while compiling class template member function 'void
boost::detail::thread_data<F>::run(void)'
monster.cpp:
#include "monster.h"
monster::monster(string temp_mob_name)
{
//New login monster
mob_name = temp_mob_name;
x=rand() % 1000;
y=rand() % 1000;
boost::thread make_thread(&monster::mob_engine);
}
monster::~monster()
{
//Destructor
}
void monster::mob_engine()
{
while(true)
{
Sleep(100);
cout<< "Monster name"<<mob_name<<endl;
}
}
monster.h:
#ifndef _H_MONSTER_
#define _H_MONSTER_
//Additional include dependancies
#include <iostream>
#include <string>
#include "boost/thread.hpp"
using namespace std;
class monster
{
public:
//Functions
monster(string temp_mob_name);
~monster();
//Custom defined functions
void mob_engine();
int x;
int y;
};
//Include protection
#endif
mob_engine is a non-static member function, so it has an implicit this argument.
Try this:
boost::thread make_thread(boost::bind(&monster::mob_engine, this));
According to this similar question boost:thread - compiler error you can even avoid using bind by simply writing:
boost::thread make_thread(&monster::mob_engine, this);
Also, you will probably want to declare a boost::thread member variable to keep a reference to the thread.

Inheritance and Combination - Base class has a pointer to derived class

I'm wondering if I can have two classes look like these:
//file: Small.h
#pragma once
#include "Little.h"
class Small :
public Little
{
public:
Small(void){}
~Small(void){}
};
and
//file: Little.h
#pragma once
#include <iostream>
#include "Small.h"
using namespace std;
//class Small;
class Little
{
public:
Little(){ s = 0; }
void print(){ cout << "oops!" << endl; }
Small* s;
};
And now my problem: When I wanna create an object of type "Small" and call its "print()" function, VS-2010 says that "class 'Small' has no member named 'print()'." What's the solution?
The following should work, however, you should be constructing classes in this manner, the base class should really have a member of the type of a derived class.
However as the member is just a pointer, the code should still compile and work, however your asking for errors.
The problem with you code is that the class Small must be defined before you create the class Little and Little bfroe you create Small
You Should do it in one file as follows, as they are interdependent
//file: SmallLittle.h
#pragma once
#include <iostream>
using namespace std;
class Small;
class Little
{
public:
Little(){ s = 0; }
void print(){ cout << "oops!" << endl; }
Small* s;
};
class Small :
public Little
{
public:
Small(void){}
~Small(void){}
};
However since Small is a member type of Little, your probably better of creating a single class as follows, The only reason for using your code is so you can use Little code in which Small is not defined, or used. However in this case your better off using a void * type for the pointer.
as Follows
//file: Small.h
#pragma once
#include <iostream>
using namespace std;
class Small;
class Small
{
public:
Small(){ s = 0; }
void print(){ cout << "oops!" << endl; }
Small* s;
~Small(void){}
};
Also you I don't think you need to set s to 0, as this is NULL, which should be the value until you create a pointer using new of assign a pointer.

Resources