0

这是我第一次将我的程序分成头文件和主文件。通过查看旧的 StackExchange 问题和答案,我必须相信我有所谓的“循环依赖”。

主.c:

#include <stdio.h>
#include "tree.h"
#include "1.h"

void main()
{
    float res;
    /* Example tree for function testing purposes */
    Tree sampleTree;


    printf("%f", sumNodes(&sampleTree));
    //res = averageOfValues(sampleTree);

    printf("The avarage value of the tree nodes is: %f", &res);

}

1.h:

#ifndef __1_H
#define __1_H

/* Prototypes */

Tree sampleTree1();
float averageOfValues(Tree tr);

#endif

1.c:

#include "1.h"
#include "tree.h"
#include <stdlib.h>

Tree sampleTree1()
    {
    Tree resTree;
    TreeNode *l3_ll, *l3_lr, *l2_l, *l2_r, *l3_rr, *root;

    l3_ll = createNewTreeNode(4, NULL, NULL);
    l3_lr = createNewTreeNode(5, NULL, NULL);
    l3_rr = createNewTreeNode(6, NULL, NULL);
    l2_l = createNewTreeNode(2, l3_ll, l3_lr);
    l2_r = createNewTreeNode(3, NULL, l3_rr);   
    root = createNewTreeNode(1, l2_l, l2_r);
    resTree.root = root;

    return resTree;
    }

float averageOfValues(Tree tr)
{
    float num; // counting the number of nodes
    float sum; // summing the data of all nodes

    num = (float)numNodes(&tr); // implicit casting (int -> float)
    sum = sumNodes(&tr);

    return sum/(float)num; 
}

树.h

#ifndef __TREE_H
#define __TREE_H

/* Type definitions */

typedef struct TreeNode{ 
    int data; 
    struct TreeNode* left; 
    struct TreeNode* right; 
} TreeNode; 

typedef struct tree{ 
    TreeNode* root; 
} Tree;

/* Prototypes */

TreeNode* createNewTreeNode(int data, TreeNode *left, TreeNode *right);
int numNodes(Tree *tr);
int numNodesHelper(TreeNode *root);
int sumNodes(Tree *tr);
int sumNodesHelper(TreeNode *root);

#endif

错误:

Error   1   error C2061: syntax error : identifier 'sampleTree1'    c:\users\user\documents\visual studio 2010\projects\ex4\q1\1.h  8   1   Q1
Error   2   error C2059: syntax error : ';' c:\users\user\documents\visual studio 2010\projects\ex4\q1\1.h  8   1   Q1
Error   3   error C2059: syntax error : ')' c:\users\user\documents\visual studio 2010\projects\ex4\q1\1.h  8   1   Q1
Error   4   error C2146: syntax error : missing ')' before identifier 'tr'  c:\users\user\documents\visual studio 2010\projects\ex4\q1\1.h  9   1   Q1
Error   5   error C2061: syntax error : identifier 'tr' c:\users\user\documents\visual studio 2010\projects\ex4\q1\1.h  9   1   Q1
Error   6   error C2059: syntax error : ';' c:\users\user\documents\visual studio 2010\projects\ex4\q1\1.h  9   1   Q1
Error   7   error C2059: syntax error : ')' c:\users\user\documents\visual studio 2010\projects\ex4\q1\1.h  9   1   Q1
Warning 8   warning C4244: '=' : conversion from 'int' to 'float', possible loss of data    c:\users\user\documents\visual studio 2010\projects\ex4\q1\1.c  27  1   Q1

我究竟做错了什么?

4

0 回答 0