identifier is undefined but code compiles

I have a very simple program.

main.cpp:

#include "io.h"

int main() {
    int firstNumber {readNumber()};
    int secondNumber {readNumber()};

    writeAnswer(firstNumber + secondNumber);
}

io.cpp:

#include "io.h"
#include <iostream>

int readNumber()
{
    int returnValue{};
    std::cout << "Please enter an integer: ";
    std::cin >> returnValue;

    return returnValue;
}

void writeAnswer(int sum)
{
    std::cout << "The result is: " << sum;
}

io.h:

#pragma once

int readNumber();
void writeAnswer(int sum);

First of all, the code compiles fine but Visual Studio complains in my main.cpp:
enter image description here

Funny thing, when I include my header file twice … the “error” is gone:
enter image description here

I’m so confused why Visual Studio complains about that? What do I wrong?

  • 3

    IntelliSense and C++ is a notoriously unreliable combination.

    – 

  • “when I include my header file twice … the “error” is gone” / “What do I wrong?” – You are doing nothing wrong. IntelliSense has caused many hours of confusion for people. Turn it off and your programming experience will be improved a lot.

    – 




  • Are you using visual studio or vs code?

    – 

  • Looks like vscode. Which version of visual studio or vscode are you using? Note that vscode and visual studio are different.

    – 

  • 1

    @TedLyngmo – Alright, thank you very much 🙂

    – 

Leave a Comment