C ++从.h文件调用函数很麻烦

时间:2019-07-14 02:22:17

标签: c++ function calling-convention

因此,我一直未能调用在.h文件中创建的函数。我希望有人能告诉我他们如何在main.cpp中引用<< A_score。基本上,A_score是在.h文件内部计算的分数。我的目标是让main.cpp显示此分数和其他类似的分数,但是我很难让main.cpp甚至无法识别我在.h文件(称为“ Call_Agreeableness_Questions”)中所做的功能。我过去取得的最大进步是没有错误,但main.cpp并未确认Call_Agreeableness_Questions甚至存在。

我也尝试查看函数原型示例和观看视频,但是从头开始编写自己的代码似乎对我没有太大帮助。我希望通过查看应该如何执行此操作,可以看到自己出了什么问题,并为我提供了我不了解的调用此函数的哪些部分的指导。调用适当的函数时,我可能不理解。

#include <iostream>
#include <fstream>
using namespace std;

//==================Function to be called================
int Call_Agreeableness_Questions(int)
//========================data types=====================
{
    int A_score_DataSet[9];
    int* address_A_score;
    int A_score = *address_A_score;
//===========Standard Big Five Personality Test Questions==========
    cout << "This is the Agreeableness section of the Big Five Personality test \nAnswer these questions using these numbers: \n1 = disagree\n2 = slightly disagree\n3 = neutral\n4 = slightly agree\n5 = agree\n\n";
    cout << "Feel little concern for others ";
    cin >> A_score_DataSet[0];
    A_score_DataSet[0] *= -1;
    cout << "Am interested in people ";
    cin >> A_score_DataSet[1];
    cout << "Insult people ";
    cin >> A_score_DataSet[2];
    A_score_DataSet[2] *= -1;
    cout << "Sympathize with others' feelings ";
    cin >> A_score_DataSet[3];
    cout << "Am not interested in other people's problems ";
    cin >> A_score_DataSet[4];
    A_score_DataSet[4] *= -1;
    cout << "Have a soft heart ";
    cin >> A_score_DataSet[5];
    cout << "Am not really interested in others ";
    cin >> A_score_DataSet[6];
    A_score_DataSet[6] *= -1;
    cout << "Take time out for others ";
    cin >> A_score_DataSet[7];
    cout << "Feel others' emotions ";
    cin >> A_score_DataSet[8];
    cout << "Make people feel at ease ";
    cin >> A_score_DataSet[9];
//===============Calculating Results===========================
    for(int x=0; x<10; ++x)
    {
    *address_A_score += A_score_DataSet[x];
    }
    cout << "Your total score for Agreeableness is "<< A_score;
//===================Creates .txt file with Results===============
    cout << "\n now I'll try to add results to Results.txt";
    ofstream myfile ("Results.txt");
    if (myfile.is_open())
    {
        myfile << "Your score is " << A_score;
        myfile.close();
    }
    cout << "YAY I think I made a file, check Agree files";
    return A_score;
}

这是主要问题,由于经历了很多失败,所以我放弃了尝试。

#include "/home/leman/code/AgreeablenessQuestions/AgreeablenessQuestions.h"

//So This file is in a different folder hence the weirdness
//with the include up top ^ there

//Basically that .h file calculates a variable I call A_Score
//All I want this main.cpp to do is to display A_Score
//I'm going to have this program display other scores similar to
//A_Score hence why I don't just cout A_Score in the included file

main(){

return 0;
}

最后,指针只是我修改A_score值的一种方式。在我没有更实际的理由使用指针之前,这只是练习使用指针的借口。 Results.txt的创建是基于我以前的一个想法,我将其保留在那里,因为这是我学习并想记住的一项新技能。实际上是不需要的。

0 个答案:

没有答案