0

I'm just learning C++. I'm following a YouTube video, but I'm stuck at this stage. Below is my files.

// main.cpp

#include <iostream>
#include "Person.h"

int main()
{
    Person foo;
    foo.set_age(25);

    std::cout << foo.get_age() << std::endl;

    return 0;
}
// Person.h

#ifndef PERSON_H
#define PERSON_H

#pragma once

class Person
{
public:
    void set_age(int a);
    int get_age();

private:
    int age;
};

#endif
// Person.cpp

#include "Person.h"

void Person::set_age(int a)
{
    age = a;
}
int Person::get_age()
{
    return age;
}

I just set up the development environment in vscode and installed code runner.

Whenever I click the Run button here

It says

launch: program '.....main.exe' does not exist

Is there a tool like php composer that automatically manages it?

Moved the contents of the .cpp file to the .h file.

It worked fine. But I would like to separate the two files for management.

halfer
  • 19,824
  • 17
  • 99
  • 186
rurumita
  • 17
  • 2
  • 5
    *I'm following an youtube video* -- That's the first mistake. Get yourself proper [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). C++ isn't like PHP -- C++ is one of the most complex computer languages in existence, and learning it through some youtube video, or thinking you can cherry pick what to learn from a crib sheet, doesn't work. Maybe that's how you can handle PHP, but C++ is a different beast altogether. – PaulMcKenzie Apr 01 '22 at 20:01
  • If you are using VSCode please note that by default it assumes you want to build a simple 1 source file executable. If you have more than 1 source file you need to read the directions at the github site on how to change your tasks.json file to support building with more than 1 source file, – drescherjm Apr 01 '22 at 20:05
  • 2
    Since you are a beginner: Did you actually compile your code prior to trying to execute it? You have to build it first. On a sidenote, you don't need the `#pragma once` if you have an include guard (the #ifndef... #define part). – kamshi Apr 01 '22 at 20:06
  • 1
    Your code looks okay. `get_age()` is problematic because your Person class does not have a proper constructor so you can call get_age() before `age` has been initialized causing undefined behavior. – drescherjm Apr 01 '22 at 20:08
  • 1
    Your problem is in whatever tool you are using to build or how you built your code. Since there are countless ways we can't help unless you tell us and show your configuration. – drescherjm Apr 01 '22 at 20:10
  • ***program '.....main.exe' does not exist*** It won't exist if the build failed or you did not build. – drescherjm Apr 01 '22 at 20:11
  • 1
    I strongly recommend that you learn to use a command-line compiler/linker, such as gcc, before you rely on any more complex build system. – Beta Apr 02 '22 at 23:03

0 Answers0