【问题标题】:c++ put functions in separate source filesc ++将函数放在单独的源文件中
【发布时间】:2016-05-30 14:47:39
【问题描述】:

目前我有一个唯一的源文件 (*.cpp),我的所有功能都可以正常工作。现在我正在尝试将其中一些放到单独的源文件中,并将它们包含在主源中,但没有成功。

我目前的项目如下:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <windows.h>
#define _SQLNCLI_ODBC_
#include <sqlext.h>
#include <sqlncli.h>

using namespace std;
using std::cout;
using std::ifstream;


/*This is one of the functions to be put in separate file:*/
string *ReadPageAsignations ( const char* RutayNombre, const char* Page )
{
bool MisionCumplida = false;
bool EncabezadoListo = false;
int i = 0;
int j = 0;
char * pch;
char istr[256];
const int NUM_DATA = 15;    
static string data[NUM_DATA];
std::stringstream InputString;
ifstream inputFile(RutayNombre); 

if (inputFile.is_open())        
  {
  while (inputFile.good() && MisionCumplida == false)
    {
        i = 0;
        inputFile.getline(istr,256);
        pch = strtok (istr,":");
        if (string(pch) == "[Pagina]")
        {
            EncabezadoListo = true;
        }
        else
        {
            EncabezadoListo = false;
        }
        if (string(pch) == Page)
        {
            MisionCumplida = true;
        }
        while (pch != NULL)
            {
                if ((EncabezadoListo == true) || (MisionCumplida == true))
                {
                    data[i] = data[i] + " " + string(pch);
                }
                pch = strtok (NULL, ",");
                i++;
            }
    }
    inputFile.close();
    return data;
  }
} //End of function 'ReadPageAsignations'

/*This is another function where my function "ReadPageAsignations' get called -- btw, I want also this function to be in a separate source file.*/
void DeliverHtml (const char* page){//const char* RutayNombre ) {
  string *p;
  char * pch;
  size_t pos;
  string RutayNombre;
RutayNombre = "../Substructure/Templates/" + SearchConfigValue( "../Substructure/Conf/Config-Templates.txt", "htmlTemplate:");
const char *RutayNombreConfigCompos = "../Substructure/Conf/Config-Composition.txt";
  string RutayNombreParaInsertar;
  string token, token1, token2;
  string line, lineRead, lineToInsert;
  char * StrToTokenize2;
  string StrToTokenize1;
p=ReadPageAsignations( RutayNombreConfigCompos, page); //Here, I call the function I want in a separate file
...
}

/*And here is the main() function*/
int main()
{
char *value = "page=Home";
    if (NULL!=strstr(getenv("QUERY_STRING"), "page="))
    {
        value = getenv("QUERY_STRING");
    }


  char *posCh = strstr(value, "=");

    DeliverHtml(&posCh[0]+1);
return 0;
}

对于第一个函数,我尝试创建头文件“ReadPageAsignations.h”和源文件“ReadPageAsignations.cpp”。

头文件“ReadPageAsignations.h”包含:

#ifndef READPAGEASIGNATIONS_H_INCLUDED
#define READPAGEASIGNATIONS_H_INCLUDED

string *ReadPageAsignations ( const char* RutayNombre, const char* Page );

#endif // READPAGEASSIGNATIONS_H_INCLUDED

单独函数的源文件“ReadPageAsignations.cpp”包含:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>


using namespace std;
using std::cout;
using std::ifstream;

string *ReadPageAsignations ( const char* RutayNombre, const char* Page )
{
bool MisionCumplida = false;
bool EncabezadoListo = false;
int i = 0;
int j = 0;
char * pch;
char istr[256];
const int NUM_DATA = 15;    /*El numero de elementos debe coincidir con el iterador en la función Deliverhtml.*/
static string data[NUM_DATA];
std::stringstream InputString;
ifstream inputFile(RutayNombre); //Abre el archivo y lo asigna al stream inputFile.

if (inputFile.is_open())        //Chequea que el archivo esté abierto.
  {
  while (inputFile.good() && MisionCumplida == false)
    {
        i = 0;
        inputFile.getline(istr,256);
        pch = strtok (istr,":");
        if (string(pch) == "[Pagina]")
        {
            EncabezadoListo = true;
        }
        else
        {
            EncabezadoListo = false;
        }
        if (string(pch) == Page)
        {
            MisionCumplida = true;
        }
        while (pch != NULL)
            {
                if ((EncabezadoListo == true) || (MisionCumplida == true))
                {
                    data[i] = data[i] + " " + string(pch);
                }
                pch = strtok (NULL, ",");
                i++;
            }
    }
    inputFile.close();
    return data;
  }
} //End function

并且,主要项目包含:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <windows.h>
#define _SQLNCLI_ODBC_
#include <sqlext.h>
#include <sqlncli.h>

#include "ReadPageAsignations.h" //Here I #include the function definition file (header)

using namespace std;
using std::cout;
using std::ifstream;
...
}

我有很多编译错误:

\ReadPageAsignations.h|4|error C2143: syntax error : missing ';' before '*'|
\ReadPageAsignations.h|4|error C4430: missing type specifier - int assumed. Note: C++ does not support default-int|
\ReadPageAsignations.h|4|error C4430: missing type specifier - int assumed. Note: C++ does not support default-int|
main.cpp|20|error C2872: 'string' : ambiguous symbol|
...

我正在使用 MS Visual C++ 2005/2008 编译器使用 Code::blocks 13.12。

任何帮助将不胜感激,在此先感谢。

【问题讨论】:

  • 你的头文件 ReadPageAsignations.h 需要包含 &lt;string&gt; 并且源文件需要包含头文件
  • 我建议您从一个较小的示例开始。使用更少的代码并尝试把它做对,然后你也知道如何在你的实际项目中做到这一点

标签: c++ function


【解决方案1】:

错误告诉您,当它尝试解析头文件时遇到符号string 并且无法识别它。将#include &lt;string&gt; 添加到您的头文件并将string 类型完全限定为std::string 应该可以解决问题。

【讨论】:

  • 恕我直言,包括标题中的所有内容都不是最好的。相反,我会在标题中包含标题所需的所有内容,而不是更多。
  • 很公平。这样肯定会减少编译时间。我会改变我的答案。
【解决方案2】:

您应该将#include &lt;string&gt; 放入您的头文件中,并将其从您的.cpp 文件中删除

如下:

main.cpp

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <windows.h>
#define _SQLNCLI_ODBC_
#include <sqlext.h>
#include <sqlncli.h>
#include "ReadPageAsignations.h"
...

注意:包含与.cpp文件同名的头文件,同时包含。

ReadPageAsignations.h

#ifndef READPAGEASIGNATIONS_H_INCLUDED
#define READPAGEASIGNATIONS_H_INCLUDED
#include <string>        //<-----This line, include string header

std::string *ReadPageAsignations ( const char* RutayNombre, const char* Page );

#endif // READPAGEASSIGNATIONS_H_INCLUDED

ReadPageAsignations.cpp

#include <iostream>
#include <fstream>
#include <sstream>
#include "ReadPageAsignations.h"  // <--- add the header file here
//#include <string>   <---remove it already included in the header file


using namespace std;
//using std::cout;  <--remove this you already used namespace std
//using std::ifstream;  <--remove this you already used namespace std

string *ReadPageAsignations ( const char* RutayNombre, const char* Page )
{

... } //End function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 2018-04-12
    • 1970-01-01
    • 2017-11-18
    • 2019-04-07
    • 1970-01-01
    相关资源
    最近更新 更多