-5

func.h

void in(){}

func.cpp

void in(){  
printf("HelloWorld");
}

main.cpp

#include "iostream"
#include "func.h"
int main(){
    in();
}

error C3861: 'printf': identifier not found

Help me to solve this problem, thanks

Adam
  • 16,808
  • 7
  • 52
  • 98
user2477
  • 896
  • 2
  • 10
  • 23
  • 1
    What's the `/#include` notation about? – Jonathan Leffler Oct 24 '13 at 06:08
  • because i cann't type #include so i type /#include instead, but it is not the problem – user2477 Oct 24 '13 at 06:10
  • for the error you get (void in(){}) remove the {} in the .h file :) – alexbuisson Oct 24 '13 at 06:17
  • Welcome to Stack Overflow. Please read the [About] page soon. Note that people expect you to provide syntactically correct code most of the time, so posting code with notations like `/#include "iostream"` makes you look a little silly. You should be using the angle brackets notation `#include ` (because that's how the standard writes it), though the double quotes do work. – Jonathan Leffler Oct 24 '13 at 06:17

1 Answers1

2

Your source file func.cpp should #include <cstdio> or perhaps #include <stdio.h> to declare printf() before you use it. With <cstdio>, you get to use namespace std, so you might write using namespace::std; after the #include line, or you might use std:: as a prefix to the function name.

You also need #include "func.h" in func.cpp.

func.cpp — Option 1

#include <cstdio>
#include "func.h"

void in()
{  
    std::printf("HelloWorld");
}

func.cpp — Option 2

#include <cstdio>
#include "func.h"
using namespace std;

void in()
{  
    printf("HelloWorld");
}

func.cpp — Option 3

#include <stdio.h>
#include "func.h"

void in()
{  
    printf("HelloWorld");
}

Option 1 is probably the preferred one.

func.h

Also, the header should declare the function, not define it, unless you really want an empty function body for the function — in which case you don't need the source file func.cpp at all.

void in();

Usually, a header should be protected against multiple inclusion. This time, it won't hurt, but it is a good idea to learn good habits:

#ifndef FUNC_H_INCLUDED
#define FUNC_H_INCLUDED

void in();

#endif /* FUNC_H_INCLUDED */
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278