为什么在没有IntelliSense错误时会出现这么多编译错误?怎么修?

时间:2012-05-13 20:32:38

标签: c struct

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

#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;
}

1 个答案:

答案 0 :(得分:2)

C ++的Intellisense众所周知是不可靠的,只是因为Intellisense的某些内容被错误报告或者没有被报告为错误。

此外,只要您有错误,必须向我们提供错误,否则我们很可能无法为您提供帮助。

有一件事是显而易见的:移动struct s的定义和上面main函数的原型,这样你就没有一堆未定义的函数和结构。