c - 当没有 IntelliSense 错误时,为什么会有这么多编译错误?怎么修?

标签 c struct

我试图在 C 中模拟继承,因此我在 Visual Studio 中创建了一个 C 文件并编写了一些代码。我确保没有 IntelliSense 错误并编译了代码,然后它告诉我有 40 多个错误。为什么之前没有提到他们?让代码正常工作的基本方法是什么? (我懂一些 Java,但不太懂 C。)

#include <stdio.h>
#include <string.h>

//Create a manager which should inherit from employee

int main(void)
{
    // construct a Manager object
    double d = 8000;
    char carl[] = "Carl";
    Manager boss= newManager(carl, d, 1987, 12, 15);
    setBonus(&boss, 5000);

    typedef union{ //typedef!?
       Employee e;
       Manager m;
      } Person;

    Person staff[3];    
    // fill the staff array with Manager and Employee objects
      staff[0].m = boss;    
      Employee harry; harry = newEmployee("Harry", 50000, 1989, 10, 1);
      staff[1].e=harry;    
      Employee tommy; tommy = newEmployee("Tommy", 40000, 1990, 3, 15); 
      staff[2].e = tommy;

      // print out information about all Employee objects
      int i;
      for (i=1;i<3;i++){
          //check if employee or manager
          Employee em; em = staff[i].e;
          printf ("%s\n", em.name); 
          printf("%s\n", 345);
      }     
}   

typedef struct {    
    char name[20]; 
    double salary;
    } Employee;

Employee newEmployee(char n[], double s, int year, int month, int day)
{
    Employee emp;
    strncpy(emp.name, n, 20);
    emp.salary=s;    
    return emp;  
}

//use pointer to change actual value
void raiseSalary(Employee (*emplo), double byPercent)  
{   
    double raise = (*emplo).salary * byPercent / 100;
    (*emplo).salary += raise;
}

//Manager struct inheriting from employee struct
typedef struct {
    Employee employee;   
    int bonus;
} Manager;      

Manager newManager(char n[], double s, int year, int month, int day)
{
    Manager man;    
    strncpy(man.employee.name, n, 20);
    man.employee.salary = s;
}

double getManagerSalary(Manager man)
{
    double basesalary = man.employee.salary;
    return basesalary + man.bonus;
}

void setBonus(Manager* man, int b)
{
    (*man).bonus = b;
}

最佳答案

C++ 的 Intellisense 是出了名的不可靠,仅仅因为 Intellisense 报告或不报告某些错误并没有多大意义。

此外,每当您遇到错误时,您都必须向我们提供错误,否则我们很可能无法为您提供帮助。

有一点是显而易见的:将 struct 的定义和 main 之上函数的原型(prototype)移至 main 之上,这样就不会出现一堆未定义的函数和结构.

关于c - 当没有 IntelliSense 错误时,为什么会有这么多编译错误?怎么修?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10574995/

相关文章:

c - C中如何交换链表节点

c - 通过函数中的指针修改结构成员

pointers - 如何在指针内改变结构中的字段?

go - 传播或解压缩结构作为测试的输入参数

c - 使用信号在父进程和多个子进程之间进行进程同步

不使用打印将ascii转换为十六进制

c - 使 malloc 自动失败以测试 malloc 失败时的情况

c# - 将结构与 WCF 服务一起使用

c - 如何释放指向保存地址的指针的指针

在 C 中从数组本身创建数组内部值的副本