C ++. Cpp file does not see variables from .h

I wrote a program in C ++. At first I wrote this normally (usually I do not write in C ++), and I wanted to put the variables in the header and code in the .cpp file. The problem is that the class in .cpp does not see variales - "Identifier undefined".

hijras

#include <iostream>
#include <string>
#include <cmath>
#include <vector>

using namespace std;

class Hex {

private:
    int n;
    string value;
    bool negative = false;

public:
     Hex();
     bool isCorrect();
     string getValue();
     void setValue();
};

a.cpp

#include "a.h"
#include "stdafx.h"     

class Hex {

    public:
      Hex(int n, string w) { //some implementation }

//rest class
}

What am I doing wrong? If this is important, I am working on VS 2013.

+4
source share
2 answers

You define your class twice, once in the header file and once in the .cpp file. Assuming you just want to declare the functions in the header file and define them in the .cpp file, this is the path: header:

#include <iostream>
#include <string>
#include <cmath>
#include <vector>

using namespace std;

class Hex {

private:
    int n;
    string value;
    bool negative;

public:
     Hex(int n, string w);
     bool isCorrect();
     string getValue();
     void setValue();
};

.cpp file:

#include "a.h"
#include "stdafx.h"     
Hex::Hex(int n, string w) : negative(false) { /*some implementation*/ }
//rest class and definitions of bool isCorrect(); string getValue(); void setValue();
+8
source

In the header, you declare it as Hex();, but in .cpp you declare it asHex(int n, string w)

: Hex::Hex(){//some implementation }

+4

Source: https://habr.com/ru/post/1584723/


All Articles