#Weird compilator problem

49 messages · Page 1 of 1 (latest)

abstract kelp
#

I use random array in two files, so i wanted to move it into new file with new class where ill add new random'ish functions. So. Compiler said:

  1. "rnd" "class" type redefinition on line 3 (C2011)
  2. use of undefined type "rnd" (C2027)
#include <random>
using namespace std;
class rnd {
public:
    int minRng, maxRng;
    rnd(int minRng, int maxRng) : minRng(minRng), maxRng(maxRng) {}

    int* generate_random_int_arr(int size) {
        int* temp_arr_GRIA = new int[size];

        std::random_device rnd;
        std::mt19937 gen(rnd());
        std::uniform_int_distribution<> distr(minRng, maxRng);

        for (int i = 0; i < size; i++) {
            temp_arr_GRIA[i] = distr(gen);
        }

        return temp_arr_GRIA;
    }
};

IDK what's going on.

tame scrollBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

latent sinew
#

is this a source file (.cpp) or header file (.h)

abstract kelp
#

no, this is a rnd.cpp file
Theres an entry point in main.cpp and whole project w/o any .h files

#

my university project.

#

ill push to git and sed link here

latent sinew
#

ok

abstract kelp
frosty summit
#

you have issue with files, not code itself

#

;compile

#include <random>
using namespace std;
class rnd {
public:
    int minRng, maxRng;
    rnd(int minRng, int maxRng) : minRng(minRng), maxRng(maxRng) {}

    int* generate_random_int_arr(int size) {
        int* temp_arr_GRIA = new int[size];

        std::random_device rnd;
        std::mt19937 gen(rnd());
        std::uniform_int_distribution<> distr(minRng, maxRng);

        for (int i = 0; i < size; i++) {
            temp_arr_GRIA[i] = distr(gen);
        }

        return temp_arr_GRIA;
    }
};
obsidian daggerBOT
#
Compilation successful
abstract kelp
#

yep, i see. so i think it's a compilator issue

frosty summit
#

no.

#

try to compile only this one file

#

g++ -c rng.cpp -o rng.obj

#

this compile only that file and not link it to executable

abstract kelp
latent sinew
#

yea, that might do it. You have 9 cpp files

frosty summit
abstract kelp
# frosty summit in console <:xd02:928339479960367135>

uh

g++ : The name “g++” is not recognized as the name of a cmdlet, function, script file, or executable program. Check that the name is spelled correctly and that the path is present and correct, then try again.
string:1 character:1
+ g++ -c rng.cpp -o rng.obj
+ ~~~
    + CategoryInfo          : ObjectNotFound: (g++:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Runned in powershell, and same in cmd

frosty summit
#

so, you use windows

latent sinew
#

What IDE and compiler do you have

frosty summit
#

run it in your compiler

abstract kelp
latent sinew
#

ah, so you're most likely using MSVC as your compiler unless you installed something else

abstract kelp
#

hm, so i have to install other compiler?

latent sinew
#

no, MSVC is pretty good

frosty summit
#

compiles without warnings/errors in godbold

#

msvc is awful

#

msvc is worst thing that can be

#

just compare popularity of windows to bad things in msvc, with linux to clang/gcc

#

clang better for windows that msvc

frosty summit
#

clang is really good, and have nice toolchain (llvm)

#

it first,

second, it would not solve your problem, i go to read code more to find issue

abstract kelp
latent sinew
frosty summit
#

you include rng as .cpp file

#

but it not how it works

#

you not supposed to include .cpp files

#

read about translation units

#

you should have declaration in .hpp file
and implementation (definition) in .cpp file

#

this is incorrect
#include "rnd.cpp"
and you have double definition of class rnd

abstract kelp
#

so, how i have to call this file?

frosty summit
#

read about translation units
and headers/cpp files itself

#

headers is preprocessor thing

abstract kelp
#

okay