VisualStudio中的C2061错误
这是我第一次将程序划分为头文件和主文件。通过阅读旧的StackExchange问题和答案,我相信我有所谓的“循环依赖” 主要条款c:VisualStudio中的C2061错误,c,compiler-errors,C,Compiler Errors,这是我第一次将程序划分为头文件和主文件。通过阅读旧的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", su
#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.c:
错误:
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
我做错了什么?复制和粘贴遇到的编译错误总是一个好主意-错误消息也很重要。问问自己,“在
1.h
中,函数原型如何知道树是什么?它可以为main.c
编译,因为该文件的#include
顺序掩盖了1.h
中缺少的include,但是请查看并思考1.c
对#include
文件的顺序。然后考虑在<代码> 1。h < /代码>中包含<代码>树> h <代码>。(保存我对以开头的实现保留宏名称的评论,直到此问题得到解决。)无论哪本书告诉您void main()
是正确的,都是由不懂C的人写的。请使用int main(void)
。(这不是你问题的原因,但你应该解决它。)@WhozCraig,你说得对。我完全误解了整个部门的x.h和x.c文件。@贵格会教徒不要自责。第一次尝试时,你一点也不遥远。总有一天我要学习。还有其他问题可能促使你考虑把它发送到CoDeEvIEW.StAccOffFuff.com,包括基思和我自己提到的那些,但是对于这个问题,你只是有点偏离。
#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;
}
#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