Pergunta

I have been trying to debug this problem for a while and quite honestly, I just can't see what I'm doing wrong.

Why is there a syntax error?

#include <iostream>;
#include <time.h>;
#include <stdio.h>;
#include <stdlib.h>;
using namespace std;


class Problem3 {
    public:
        bool isPrime(long double num) {
            srand(time(NULL));
            return 0;
        }
};

The error I'm getting is,

"Function 'srand' could not be resolved."

  • I'm well aware now that I don't need the semi-colons after 'include' statements
  • I'm using Eclipse CDT along with MinGW as my compiler

How I resolved the problem:

It had to do with the MinGW compiler I was using. Switching over to Visual Studio solved the problem.

Foi útil?

Solução

; at the end of the #include directives are the problem in your code. #include directives don't need (wrong to place indeed) semicolons at the end unlike C++ statements.

[Warning] extra tokens at end of #include directive [enabled by default] 

It seems any character after > in the directive causes this error/warning.

          #include<iostream>a   //error

Change to this:

#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;


class Problem3 {
    public:
        bool isPrime(long double num) {
            srand(time(NULL));
            return 0;
        }
};

int main(){
    cout<<"Hello Main";
}

EDIT:

Regarding the linker issue:

One suggestion is C++ expects types to be explicitly casted between types (more than C). So, use a cast to convert time_t which is returned by the time to unsigned int which is the input parameter type of srand. (And of course this might not be the problem with linker error)

Instead of using stdlib.h, try using <cstdlib>, try if it helps. Because it uses namespace.

Apart from that, I have seen this snippet here. Use that pattern if it helps.

#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
int main() 
{
    srand(time(0)); //use current time as seed for random generator
    int random_variable = rand();
    cout << "Random value on [0 " << RAND_MAX << "]: " 
              << random_variable << '\n';
}

there is already question in SO check if that helps Eclipse Method could not be resolved in a simple program C++

Never use time() to initialize srand()..

EDIT:

Now it seems many people got this kind of problem. I found a question How do I fix Eclipse CDT Error “Function 'isdigit' could not be resolved. He is facing the same problem. The asker suggested a work around to this in his question edit.

Quoted from that question:

I now believe this to be a Code Analysis problem. A better solution is to edit the Code Analysis options to make "Function could not be resolved" be a warning instead of an error. That way you can see the warnings in Problems view, but continue to work. If the function is REALLY missing, the compiler will tell you! I also have a new theory, that the problem is with the Code Analyzer following symlinks, because all of the "missing" functions are in symlinked include files. Would love any input on this theory.

Hope that points to solve the problem.

Outras dicas

; should not be there after #include.

#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

#include files shoule not end with ;

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top