Meng
Meng

Reputation: 37

"time" is not a member of "std"

An error showed up saying "time" is not a member of "std" for the sentence:

std::srand(std::time(0));

<ctime> and <cstdlib> already included. And the compiler is TDM-GCC MinGW.

I met this error several times and I still can't figure out the reasons.

Upvotes: 4

Views: 6950

Answers (1)

Richard
Richard

Reputation: 8920

This is because time(2) is a C standard library function, not a C++ standard library function.

#include <iostream>
#include <ctime>

int main(int argc, char *argv[])
{
    auto t = time(nullptr);
    std::srand(t);
    std::cout << t << "\n";
    return 0;
}

Upvotes: 1

Related Questions