我无法弄清楚C++语法错误"expected `;' before ‘{’ token"
I can't figure out C++ syntax error "expected `;' before ‘{’ token"
我有这个简单的代码,我得到一个语法错误:
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++的新手,找不到语法问题。这是我第一次创建构造函数。
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;
}
如果你想在函数内部定义一个类,你必须在类定义中内联定义该类的每个方法。例如:
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;
}
};
既然你是c++的新手,我猜,虽然另一个答案会让你的代码编译你可能想尝试使用类头和实现文件的更标准的方式,这将让你习惯于有可重用的类,而不是类定义在main()(或其他函数)
只需将类声明移动到一个名为"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
Patient.cpp:
#include "Patient.h"
Patient::Patient () {
name="string";
height=0;
weight=0;
}
Patient::Patient(std::string n) {name=n;height=0;weight=0;}
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;
}
如果你在IDE中工作,它会为你编译main.cpp和Patient.cpp;如果您在命令行中使用g++或clang,请确保在编译时包含两个。cpp文件,例如
$ g++ main.cpp Patient.cpp -o myPatientProgram
…然后你可以运行。/myPatientProgram来查看你的程序运行情况。
hth
相关文章:
- Arduino无法编译名为SP:"Expected ')' before '(' token"的变量
- 为什么我的功能在使用 goto 时会给我带来"expected primary-expression before '}' token"?
- 使用结构和数组时的"expected primary expression before '.' token"
- 如何解决"Expected expression before " = " token"?
- 错误:创建模板类时"expected unqualified-id before '{' token"
- 如何解决"expected constructor, destructor, or type conversion before ‘(’ token"错误?
- 这个编译器错误是什么意思 - "qualified-id in declaration before ‘=’ token" C++?
- 获取链表上的"expected ‘)’ before ‘&’ token"到 BST 构造函数签名
- 使用宏访问指向数据成员的指针会导致"error: expected unqualified-id before ‘*’ token"
- "error: expected primary-expression before '<=' token" 我做错了什么?
- 内存集调用位置的"error: expected constructor, destructor, or type conversion before '(' token"
- 设置if流,获取'error: expected initializer before ‘.’ token'
- 在2D伊辛模型上工作。C++新手。我在第 23 行收到错误"expected unqualified-id before '{' token"
- 我的代码看起来不错。为什么我要得到"error: expected primary-expression before ')' token"?
- 如何修复"Expected Primary-expression before ')' token"错误?
- C++错误"tabulate.cc:19: error: expected primary-expression before ',' token"
- 使用多个文件时"array bound is not an integer constant before ']' token"
- "expected initializer before '<' token"尝试模板成员专用化
- 为什么海湾合作委员会说"a function-definition is not allowed here before '{' token"?
- C++:"error: expected ',' or '...' before '(' token"