简体   繁体   English

C++无法打开源文件

[英]C++ cannot open source file

In C++ with Visual studio 2017,在 C++ 和 Visual Studio 2017 中,

I copied some header files into my project folder, then added them under the "solution explorer" in c++.我将一些头文件复制到我的项目文件夹中,然后将它们添加到 C++ 中的“解决方案资源管理器”下。 Now when I write现在当我写

#include "name.h"

it prints an error under include, and says "cannot open source file".它在包含下打印一个错误,并说“无法打开源文件”。

Why, and what can I potentially do to fix it?为什么,我可以做些什么来修复它?

I only just downloaded VS and am learning c++ for the first time.我刚刚下载了 VS 并且是第一次学习 C++。

如果您使用的是 Visual Studio,请右键单击项目,然后单击“属性”,在“配置属性”下单击“C\\C++”,然后将目录添加到“附加包含目录”部分下的头文件中。

There is more information here on how to deal with this problem: Where does Visual Studio look for C++ header files?这里有更多关于如何处理这个问题的信息: Visual Studio 在哪里寻找 C++ 头文件?

For me, I followed xflowXen's answer and then at "Include Directories" typed in the specific pathname where my header file was located followed by a semicolon, something like: C:\\Users\\name\\source\\repos\\p2-A\\p2-A;对我来说,我按照 xflowXen 的回答,然后在“包含目录”中输入我的头文件所在的特定路径名,后跟一个分号,例如:C:\\Users\\name\\source\\repos\\p2-A\\p2-一种; then applied the changes and the issue went away.然后应用更改,问题就消失了。

Visual Studio (or rather the compiler) needs to know where to look for the included file. Visual Studio(或者更确切地说是编译器)需要知道在哪里查找包含的文件。 Check out your include path in your VS project.检查 VS 项目中的包含路径。

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

int divide(int num, int den) 
{
   if(den==0) 
   { 
      return -1; 
   } 
   if((num%den)==0) 
   { 
      return 1; 
   } 
   else 
   {    
      return 0; 
   } 
} 

int divide(int a) 
{ 
   int j = a/2, flag = 1, i; 

   for(i=2; (i<=j) && (flag); i++) 
   { 
      if(a%i == 0) 
      { 
         flag = 0; 
      } 
   } 
   return flag; 
} 

void main() 
{ 
   clrscr(); 
   int choice, res, a, b; 

   do 
   { 
      cout<<"1.Check for divisibility\n"; 
      cout<<"2.Check for Prime\n"; 
      cout<<"3.Exit\n"; 
      cout<<"Enter your choice(1-3): "; 
      cin>>choice; cout<<"\n"; 
      switch(choice) 
      { 
         case 1: 
            cout<<"Enter numerator and denominator: "; 
            cin>>a>>b; 
            res = divide(a, b); 
            if(res == -1) 
            { 
               cout<<"Divide by zero error..!!\n"; break; 
            } 
            cout<<((res) ? "It is" : "It is not")<<"\n"; 
            break; 
         case 2: 
            cout<<"Enter the number: "; 
            cin>>a; 
            res = 0; 
            res = divide(a); 
            cout<<((res) ? "It is" : "It is not")<<"\n"; 
            break; 
         case 3: 
            cout<<"Exiting...press any key..."; 
            getch(); 
            exit(1); 
         default:
            cout<<"Wrong choice..!!"; 
      } 
      cout<<"\n"; 
   }while(choice>0 && choice<=3); 
   getch(); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM