A
A
Artem2022-01-08 20:08:09
C++ / C#
Artem, 2022-01-08 20:08:09

How to use namespace in header?

solution.cpp

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

#include "input.h"

int main()
{
  setlocale(0, "");

  input::do_input();
  const uint8_t m = input::m, n = input::n;

  return 0;
}

input.h

namespace input
{
  void do_input();
  extern uint8_t m;
  extern uint8_t n;
  extern std::vector<std::vector<bool>> field;
}

input.cpp

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

using std::cout, std::cin;
using std::string, std::vector;

string buf;
uint8_t real_m, real_n;
uint8_t m, n;
vector<vector<bool>> field(m, vector<bool>(n));

void read_unuseful_line()
{
  for (uint8_t i = 0; i < n; i++)
  {
    cin >> buf;
  }
}

void do_input()
{
  cin >> buf;
  real_m = stoi(buf);
  m = real_m - 2;

  cin >> buf;
  real_n = stoi(buf);
  n = real_n - 2;

  read_unuseful_line();
  for (uint8_t i = 0; i < m; i++)
  {
    cin >> buf;
    for (uint8_t j = 0; j < n; j++)
    {
      cin >> buf;
      field[i][j] = stoi(buf);
    }
    cin >> buf;
  }
  read_unuseful_line();


  for (auto row : field)
  {
    for (auto col : row)
    {
      cout << col << ' ';
    }
    cout << '\n';
  }
}

Compiling
c++ solution.cpp input.cpp -static
Error Message
undefined reference to `input::do_input()
undefined reference to `input::m'
undefined reference to `input::n'
collect2.exe: error: ld returned 1 exit status

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2022-01-08
@Trame2771

Wrap all code in input.cpp in a namespace input {}
namespace appends the namespace to the characters in the file object, but because If you have definitions in input.cpp not included in the namespace, then in the object file these symbols will be without adding the namespace, and therefore undefined reference.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question