C++ 我能';t算出C++;语法错误“;期望`';在‘之前;{&x2019;代币“;
我有一个简单的代码,出现语法错误:C++ 我能';t算出C++;语法错误“;期望`';在‘之前;{&x2019;代币“;,c++,syntax,constructor,C++,Syntax,Constructor,我有一个简单的代码,出现语法错误: file.cc:67: error: expected `;' before ‘{’ token file.cc:73: error: expected primary-expression before ‘(’ token file.cc:73: error: expected primary-expression before ‘n’ file.cc:73: error: expected `;' before ‘{’ token 我把星星放在67和73
file.cc:67: error: expected `;' before ‘{’ token
file.cc:73: error: expected primary-expression before ‘(’ token
file.cc:73: error: expected primary-expression before ‘n’
file.cc:73: error: expected `;' before ‘{’ token
<>我把星星放在67和73行,这是类的构造函数。我对C++很陌生,并且找不到语法问题。这是我第一次做构造函数。< /P>
int main() {
class Patient {
private: // default is private but stating explicitly here for learning purposes
std::string name; //object
int height; //object
int weight; //object
public:
Patient(); //constructor
Patient(std::string); //constructor
void set_name (std::string n) {name=n;} //fn
void set_height (int h) {if (height>0){height=h;} else {height=0;}} //fn
void set_weight (int w) {if (weight>0){weight=w;} else {weight=0;}} //fn
std::string get_name(){return name;} //fn
int get_height(){return height;} //fn
int get_weight(){return weight;} //fn
int bmi(void) {
if ((height!=0) && (weight!=0))
return (weight/(height*height));
else
return 0;
}
};
Patient::Patient () { // **LINE 67**
name="string";
height=0,
weight=0;
}
Patient::Patient(std::string n) {name=n;height=0;weight=0;} // **LINE 73**
Patient father("Andrew Nonymous");
Patient mother("Ursula N. Known");
Patient baby;
//Father's height and weight are unknown.
mother.set_name("Ursula N. Nonymous");
mother.set_height(1.65);
mother.set_weight(58);
baby.set_height(0.495);
baby.set_weight(3.4);
std::cout << "Baby: " << baby.get_name() << " BMI: " << baby.bmi() << std::endl;
std::cout << "Mother: " << mother.get_name() << " BMI: " << mother.bmi() << std::endl;
std::cout << "Father: " << father.get_name() << " BMI: " << father.bmi() << std::endl;
return 0;
}
intmain(){
班级病人{
private://默认值为private,但出于学习目的在此处明确说明
std::string name;//对象
int height;//对象
int-weight;//对象
公众:
Patient();//构造函数
患者(std::string);//构造函数
void set_name(std::string n){name=n;}//fn
void set_height(inth){if(height>0){height=h;}else{height=0;}}//fn
void set_weight(int w){if(weight>0){weight=w;}else{weight=0;}}//fn
std::string get_name(){return name;}//fn
int get_height(){return height;}//fn
int get_weight(){return weight;}//fn
int bmi(无效){
如果((高度!=0)和&(重量!=0))
返回(重量/(高度*高度));
其他的
返回0;
}
};
患者::患者(){//**第67行**
name=“string”;
高度=0,
权重=0;
}
Patient::Patient(std::string n){name=n;height=0;weight=0;}/**第73行**
耐心的父亲(“安德鲁·诺尼莫斯”);
患者母亲(“Ursula N.已知”);
病人婴儿;
//父亲的身高和体重不详。
母亲的名字(“Ursula N.Nonymous”);
母亲身高(1.65);
母亲体重(58);
婴儿身高(0.495);
婴儿。设定体重(3.4);
std::cout如果要在函数中定义类,必须在类定义中内联定义该类的每个方法。例如:
class Patient {
private: // default is private but stating explicitly here for learning purposes
std::string name; //object
int height; //object
int weight; //object
public:
Patient()
{
name="string";
height=0;
weight=0;
}
};
<> P>因为您是C++的新手,我猜想,另一个答案会让您的代码编译,您可能想尝试使用类头和实现文件的更标准的方式,这将使您习惯于使用可重用类而不是在主()或其他函数中定义的类。
只需将类声明移动到名为“Patient.h”的文件中,并将实现(函数的定义)移动到“Patient.cpp”即可。在主文件和Patient.cpp中,都包括Patient.h
这是一个病人。h:
#ifndef Patient_h
#define Patient_h
#include <string>
class Patient {
private: // default is private but stating explicitly here for learning purposes
std::string name; //object
int height; //object
int weight; //object
public:
Patient(); //constructor
Patient(std::string); //constructor
void set_name (std::string n) {name=n;} //fn
void set_height (int h) {if (height>0){height=h;} else {height=0;}} //fn
void set_weight (int w) {if (weight>0){weight=w;} else {weight=0;}} //fn
std::string get_name(){return name;} //fn
int get_height(){return height;} //fn
int get_weight(){return weight;} //fn
int bmi(void) {
if ((height!=0) && (weight!=0))
return (weight/(height*height));
else
return 0;
}
};
#endif
还有main.cpp中剩下的内容:
#include <iostream>
#include "Patient.h
int main() {
Patient father("Andrew Nonymous");
Patient mother("Ursula N. Known");
Patient baby;
//Father's height and weight are unknown.
mother.set_name("Ursula N. Nonymous");
mother.set_height(1.65);
mother.set_weight(58);
baby.set_height(0.495);
baby.set_weight(3.4);
std::cout << "Baby: " << baby.get_name() << " BMI: " << baby.bmi() << std::endl;
std::cout << "Mother: " << mother.get_name() << " BMI: " << mother.bmi() << std::endl;
std::cout << "Father: " << father.get_name() << " BMI: " << father.bmi() << std::endl;
return 0;
}
…然后您可以运行./myPatientProgram以查看您的程序运行
hth您不能在其他函数中声明(或定义)类/函数。这不完全正确。@PawełStawarz是的,您可以定义类和声明函数。您不能在函数中定义独立函数,但可以在函数中定义类及其成员函数。@πάνταῥεῖ - 是合法的,即使在旧版本的标准中也是如此。问题是声明/定义的拆分。类/结构匿名也是合法的:。(在C++98中对这些类型的处理有一些限制,但最近有所取消).这是一个很好的建议,但实际上这是一项任务,如果我改变了他们想要的方式,他们会很生气。不过,我知道使用头文件将是我未来的一部分。
#include <iostream>
#include "Patient.h
int main() {
Patient father("Andrew Nonymous");
Patient mother("Ursula N. Known");
Patient baby;
//Father's height and weight are unknown.
mother.set_name("Ursula N. Nonymous");
mother.set_height(1.65);
mother.set_weight(58);
baby.set_height(0.495);
baby.set_weight(3.4);
std::cout << "Baby: " << baby.get_name() << " BMI: " << baby.bmi() << std::endl;
std::cout << "Mother: " << mother.get_name() << " BMI: " << mother.bmi() << std::endl;
std::cout << "Father: " << father.get_name() << " BMI: " << father.bmi() << std::endl;
return 0;
}
$ g++ main.cpp Patient.cpp -o myPatientProgram