0

I'm getting 238 LNK2005 errors and I have no idea what to do. They're all similar to this one:

Error LNK2005 "class wxString binary::add" (?add@binary@@3VwxString@@A) already defined in app.obj C:\Users\giorg\Desktop\Visual Studio\wxWidgetsTest\wxWidgetsTest\expandMenu.obj 1 ikeCalculator

These are the two interested files.

ops.h

#pragma once
#include "wx/wx.h"
#include <cmath>
#include <map>

namespace binary {
    extern const wxString add, sub, mul, div;
}
namespace unary {
    extern const wxString sqrt, sin, cos, tan, arcsin, arccos, arctan;
}
namespace constant {
    extern const wxString pi, light_speed, avogadro_number, atomic_mass_unit, planck, gas, gravity_acceleration, golden_ratio;
}

namespace calc
{
    extern const std::map < wxString, std::function<long double(long double, long double)> > binary;
    extern const std::map < wxString, std::function<long double(long double)> > unary;
    extern const std::map < wxString, long double > constant;
}

bool isBinary(wxString needle);
bool isUnary(wxString needle);
bool isConstant(wxString needle);

ops.cpp

#include "ops.h"

namespace binary {
    const wxString add(L"+"), sub(L"-");
    const wxString mul(L"\u00D7"), div(L"\u00F7");
    const wxString pow(L"^");
}
namespace unary {
    const wxString sqrt(L"\u221A");
    const wxString sin(L"sin"), cos(L"cos"), tan(L"tan");
    const wxString arcsin(L"arcsin"), arccos(L"arccos"), arctan(L"tan");
}
namespace constant {
    const wxString pi(L"\u03C0");
    const wxString light_speed(L"c");
    const wxString avogadro_number(L"N\u1D00");
    const wxString atomic_mass_unit(L"u");
    const wxString planck(L"h");
    const wxString gas(L"R");
    const wxString gravity_acceleration(L"g");
    const wxString golden_ratio(L"\u03D5");
}

namespace calc
{
    const std::map < wxString, std::function<long double(long double, long double)> > binary
    {
        {binary::add, [](long double a, long double b) { return a + b; }},
        {binary::sub, [](long double a, long double b) { return a - b; }},
        {binary::mul, [](long double a, long double b) { return a * b; }},
        {binary::div, [](long double a, long double b) { return a / b; }},
        {binary::pow, [](long double a, long double b) { return pow(a, b); }}
    };

    const std::map < wxString, std::function<long double(long double)> > unary
    {
        {unary::sqrt, [](long double n) { return sqrt(n); }},
        {unary::sin, [](long double n) { return sin(n * 3.14159265359 / 180); }},       //trasformazione da rad a gradi
        {unary::cos, [](long double n) { return cos(n * 3.14159265359 / 180); }},
        {unary::tan, [](long double n) { return tan(n * 3.14159265359 / 180); }},
        {unary::arcsin, [](long double n) { return asin(n) * 180 / 3.14159265359; } },  //trasformazione da rad a gradi
        {unary::arccos, [](long double n) { return acos(n) * 180 / 3.14159265359; } },
        {unary::arctan, [](long double n) { return atan(n) * 180 / 3.14159265359; } }
    };

    const std::map < wxString, long double > constant
    {
        {constant::pi, 3.14159265359},
        {constant::light_speed, 299792458},
        {constant::avogadro_number, 6.02214086 * pow(10, 23)},
        {constant::atomic_mass_unit, 1.67 * pow(10, -27)},
        {constant::planck, 6.626 * pow(10, -34)},
        {constant::gas, 8.314},
        {constant::gravity_acceleration, 9.80665},
        {constant::golden_ratio, 1.6180339887}
    };
}


bool isBinary(wxString needle)
{
    return (calc::binary.count(needle));
}

bool isUnary(wxString needle)
{
    return (calc::unary.count(needle));
}

bool isConstant(wxString needle)
{
    return (calc::constant.count(needle));
}

My Visual Studio Solution:

My Visual Studio Solution

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
iKebab897
  • 73
  • 2
  • 7
  • And you didn't accidentally `#include ` instead of `#include ` anywhere? – Scheff's Cat Jun 29 '21 at 09:26
  • Nope. I used `#include "ops.h"` where it was needed. – iKebab897 Jun 29 '21 at 09:35
  • Is it a single project, or is this a solution with multiple projects for DLLs and one EXE? – Scheff's Cat Jun 29 '21 at 09:37
  • It's a solution with a single program and multiple files (don't know if I used the right words so [here's a pic](https://ibb.co/rMXbRt5)). – iKebab897 Jun 29 '21 at 10:00
  • That sounds weird. Did you try a full rebuild (or even a Clean and then Rebuild)? Not that there are straying `.obj`s from earlier attempts or renamed files... – Scheff's Cat Jun 29 '21 at 10:08
  • It seems namespaces including constants are good candidates to be `struct` and be in a header file. – macroland Jun 29 '21 at 10:42
  • Also check this: https://stackoverflow.com/questions/10422034/when-to-use-extern-in-c – macroland Jun 29 '21 at 10:47
  • So I should replace `namespace`'s with `struct`'s? Should I put their definitions in the header file and use the .cpp just for the maps? How should I do that? – iKebab897 Jun 29 '21 at 12:47
  • It is of course a coding style; but frankly I dont see the point of referring to constants with `extern`. Just change `namespace constant {` to `struct constant {` and carry all the definitions. Then you dont need `const std::map < wxString, long double > constant`. My 2 cents is, your code looks way too "complicated" for its purpose. – macroland Jun 29 '21 at 14:50
  • I need the maps do to the calculations, and the wxString's work as some sort of ID for each operation so I don't have to repeat the Unicode character for the buttons. How do I replicate this with the method you say? – iKebab897 Jun 29 '21 at 15:54
  • Also, `struct`'s are not working: E0079 expected type identifier. – iKebab897 Jun 29 '21 at 18:15
  • How am I supposed to use structs in this case? Nothing is working and I'm going crazy because I've been stuck for 3+ days on this. – iKebab897 Jun 29 '21 at 19:25
  • @iKebab897, can you post your original code? The original error means that you define all those namespaces twice. You need to undo everythingand do a global search in the project to make sure that all those namespaces are defined only once. – Igor Jun 29 '21 at 21:08
  • What do you mean by "original code"? – iKebab897 Jun 30 '21 at 14:18
  • @ikebab897, what i mean is - go back to the very first code you used and try to search the whole project for the occurrence of that names. – Igor Jun 30 '21 at 15:49
  • Namespaces aren't defined more than once, cannot seem to be finding other definitions in my solution. The thing that's even weirder is that Icreated another project and pasted my code, replaced wxString's with std::string, and it was working fine there. – iKebab897 Jun 30 '21 at 17:24

0 Answers0