从单独的CPP文件调用函数/变量

时间:2011-04-09 17:14:40

标签: c++ include

我必须在我正在编写的程序中使用不同的CPP文件,而在我的主文件中,我从我的IsValid cpp文件中调用算法。我的IsValid算法还需要从我的主文件,电子邮件中获取变量,然后我将通过我编写的循环运行它。

我想知道如何实现这一点,因为如果我听说包含cpp文件是不好的编码实践,如果我这样做会导致包含循环。有没有人对如何解决这个问题有任何建议。

以下代码:

#include <iostream>
#include <fstream>
#include "Validation.h"

using namespace std;

#define MAX_SIZE 260
#define NOT_FOUND -1


void main()
{
    //High level alg

    // O) Open input file and output file
    ifstream input("Email.txt");
    ofstream output("Result.txt");

    // Oa) While not at the end of input
    while(!input.eof())
    {
        char email[MAX_SIZE];
        Validation A(email);
        // Ob)Read email input
        input.getline(email, MAX_SIZE);

        // Validat email
        if (A.Valid(email))

        // Write results to output file
        output<< "1" << endl;
        else
        output << "0" << endl;
    }

    system("pause");
}

第二档:

#include <iostream>
#include <string>
#include "Validation.h"

using namespace std;

#define NOT_FOUND -1

bool IsValid(char* email)
{
    int Length, atIndex, LocalLength, DomainLength, aCheck = 0;
    char* Invalid = "()[]\\;:,<>";

    // Searches for the NULL terminator at the end of the char array and returns Length of the arry
             Validation a(email);
    Length = a.GetLength;

    if(Length <= 256)
    {
        // Finds the @ char and returns an int variable labeling its position
        Validation b(email, Length);

        atIndex = b.Find;

        aCheck++;

        if(atIndex != NOT_FOUND)
        {
            Validation c(email, atIndex);

            LocalLength = c.CheckLengthLocal;
            DomainLength = c.CheckLengthDomain;

            aCheck++;

            if(LocalLength <= 64 && DomainLength <= 253)
            {
                aCheck++;
                Validation d(email, Invalid, atIndex);

                if(d.ValidCharsL == true && d.ValidCharsD == true)
                {
                    aCheck++;
                    Validation e(email, atIndex, Length);

                    if(c.CheckRuleLocal== true && e.CheckRuleDomain == true)
                    {
                        aCheck++;

                        return true;
                    }
                }
            }
        }
    }
    if(aCheck != 5)
        return false;
}

第三档:

#define MAX_SIZE 260
#define NOT_FOUND -1

#include <iostream>

#pragma once

using namespace std;

struct Validation
{
    //Returns the length of text by scanning for null-term
int GetLength(char* text)
{
    int i;
    for(i = 0; text[i] != '\0'; i++)
    {

    }
    return i;
}

Validation::Validation(char* email)
{
    char* emailC = email;

}

Validation::Validation(char* email, int Length)
{
    char* emailC = email;
    int Size = Length;
}

Validation::Validation(char* email, int atIndex, int Length)
{
    char* emailC = email;
    int Size = Length;
    int IndA = atIndex;
}

Validation::Validation(char* email, char* Invalid, int atIndex)
{
    char* emailC = email;
    char* InNum = Invalid;
    int IndA = atIndex;
}

bool Valid(char * email)
{
    char* emailT = email;
}

bool CheckRuleLocal(char* text, int Achar)
{
    int invalid = 0;
    if(text[0] == '.'|| text[Achar - 1] == '.')
        invalid++;
    if(text[0] == '-'|| text[Achar - 1] == '-')
        invalid++;
    for(int i = 0; i < Achar && invalid == 0; i++)
    {
        if(text[i] == '.' && text[i + 1] == '.')
            invalid++;
        if(text[i] == '-' && text[i + 1] == '-')
            invalid++;
    }

    if(invalid > 0)
        return false;
    else
        return true;
}

bool CheckRuleDomain(char* text, int Achar, int length)
{
    int invalid = 0;
    if(text[Achar + 1] == '.'|| text[length - 1] == '.')
        invalid++;
    if(text[Achar + 1] == '-'|| text[length - 1] == '-')
        invalid++;
    for(int i = Achar + 1; i < MAX_SIZE && invalid == 0; i++)
    {
        if(text[i] == '.' && text[i + 1] == '.')
            invalid++;
        if(text[i] == '-' && text[i + 1] == '-')
            invalid++;
    }

    if(invalid > 0)
        return false;
    else
        return true;
}


bool ValidCharsL(char* text, char* invalidCharacters, int Achar)
{
    int invalid = 0;

    for(int i = 0; i < Achar && invalid == 0; i++)
    {
        for(int t = 0; t < GetLength(invalidCharacters); t++)
        {
            if(text[i] == invalidCharacters[t])
                invalid++;
        }
    }
    if(invalid > 0)
        return false;
    else
        return true;
}

bool ValidCharsD(char* text, char* invalidCharacters, int Achar)
{
    int invalid = 0;

    for(int i = Achar + 1; text[i] != '\0'; i++)
    {
        for(int t = 0; t < GetLength(invalidCharacters); t++)
        {
            if(text[i] == invalidCharacters[t])
                invalid++;
        }
    }
    if(invalid > 0)
        return false;
    else
        return true;
}

//Finds the position of @ and returns an int  that will store value if not found returns -1
int Find(char* text, int arraySize)
{
    int AIndex = 0;
    for(int i = 0; i <= arraySize; i++)
    {
        if(text[i] == '@')
            AIndex = i;
    }
        if(AIndex > 0)
            return AIndex;
        else
            return NOT_FOUND;

}


int CheckLengthLocal(char* text, int Achar)
{
    int count = 0;
    for(int i = 0; i != Achar; i++)
    {
        count ++;
    }
    return count;
}

int CheckLengthDomain(char* text, int Achar)
{
    int count = 0;
    for(int i = Achar + 1; text[i] != '\0'; i++)
    {
        count ++;
    }
    return count;
}
};

2 个答案:

答案 0 :(得分:0)

通常不赞成将单独的单元和功能与全局变量结合在一起。但是,如果你做一种方法是在头文件中使用extern声明,并在你的一个cpp文件中声明实际变量。

答案 1 :(得分:0)

为cpp文件创建头文件,并将要公开的函数原型放到其中的其他单元中。您可以将实际实现保留在cpp文件中。包含需要编译头的cpp单元中的头文件。

我没有查看您的所有代码,但看起来您没有想到的单独变量。从main传递的变量是实变量。原型中使用的名称只是一个替换的占位符。需要编译单元,链接器将对其进行排序。

也许从一个更简单的东西开始,一个或两个函数在一个单独的单元中,直到你掌握它?