Alex Kougedakos
Alex Kougedakos

Reputation: 1

Issues with classes in C++

I started taking a C++ course but I am stuck on the classes. I copied the code exactly and it does not seem to work. I've had this issue for 3 days now and I've tried a lot of things like switching compliers and creating new projects. If anyone knows what the issue is please help me.

It also is not giving me any errors. It just does not launch at all.

//main.cpp
#include <iostream>
#include "Person.h"
using namespace std;

int main()
{
    Person person;

    cout << person.toString() << endl;
    return 0;
}
//person.cpp
#include "Person.h"

Person::Person()
{
    name = "George";
}

string Person::toString()
{
    return "Person's name is " + name;
}

//person.h
#pragma once
#ifndef PERSON_H
#define PERSON_H

#include <iostream>
#include <string>
using namespace std;

class Person
{
private:
    string name;
             

public:
    Person();
    string toString();
};


#endif // !1

Upvotes: 0

Views: 123

Answers (2)

רן מורסקי
רן מורסקי

Reputation: 134

I'm running windows

I opened VS 2019 new project and added your files and it worked... I think you have a problem with how you open the project in your IDE (framework), try to see a tutorial about how to open the project in your IDE

this is tutorial to open a VS 2019 project: https://youtu.be/jUrMD1zfGVc

good luck!

Upvotes: 1

SYAOnline
SYAOnline

Reputation: 11

Maybe there is an issue on how you build it.

I'm running Linux, so this is what I've did:

Wrote the code to this files:

main.cpp  Person.cpp  Person.h

Compiled the code:

~/dev/cpp$ g++ -Wall -Wextra -Werror -o app Person.cpp main.cpp 
~/dev/cpp$ ./app 

This is the output:

Person's name is George

Upvotes: 1

Related Questions