新手:C++出现 “People::getAnswer”: 非标准语法;请使用 "&" 来创建

qq_41647611 2018-04-20 07:18:12
class People
{
private:int myweight;
double myheight;
double mybmi;
public:
People() :myweight(0), myheight(0.0), mybmi(0.0){}
People(int weight,double height):myweight(weight),myheight(height){}
void calculatebmi() { mybmi= myweight / myheight * myheight; }
void getAnswer(){
calculatebmi();
if (mybmi > 23.9)cout << "Yes!" << endl;
else cout << "No!" << endl;
}
};
C++出现 “People::getAnswer”: 非标准语法;请使用 "&" 来创建
请问应该怎样修改我的代码呢?
谢谢各位大佬了
...全文
1657 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
棉猴 2018-07-06
  • 打赏
  • 举报
回复
VS2015运行没问题
  • 打赏
  • 举报
回复
额!没有(),我以前也这样干过
JackyRao 2018-07-05
  • 打赏
  • 举报
回复
这种问题就不要问了
「已注销」 2018-05-04
  • 打赏
  • 举报
回复
#5 代码测试正常
qq_40162781 2018-04-26
  • 打赏
  • 举报
回复
多了个返回0吧??。。。
KCNKCN 2018-04-21
  • 打赏
  • 举报
回复
按照版主的办法,问题解决了么? 这么简单的类,函数能少点不?函数定义和函数声明按照格式来吧,这代码看着好别扭。
paschen 版主 2018-04-20
  • 打赏
  • 举报
回复
a.getAnswer; 改成: a.getAnswer();
qq_41647611 2018-04-20
  • 打赏
  • 举报
回复
#include "stdafx.h" #include <iostream> using namespace std; class People { private:int myweight; double myheight; double mybmi; public: People() :myweight(0), myheight(0.0), mybmi(0.0){} People(int weight,double height):myweight(weight),myheight(height){} void calculatebmi() { mybmi= myweight / myheight * myheight; } void getAnswer(){ calculatebmi(); if (mybmi > 23.9)cout << "Yes!" << endl; else cout << "No!" << endl; } }; int main() { People a = People(); int weight; double height; cin >> weight >> height; a = People(weight, height); a.getAnswer(); return 0; } 是这个
qq_41647611 2018-04-20
  • 打赏
  • 举报
回复
#include <iostream> using namespace std; class People { private:int myweight; double myheight; double mybmi; public: People() :myweight(0), myheight(0.0), mybmi(0.0){} People(int weight,double height):myweight(weight),myheight(height){} void calculatebmi() { mybmi= myweight / myheight * myheight; } void getAnswer(){ calculatebmi(); if (mybmi > 23.9)cout << "Yes!" << endl; else cout << "No!" << endl; } }; int main() { People a = People(); int weight; double height; cin >> weight >> height; a = People(weight, height); a.getAnswer; cin >> weight >> height; return 0; }
paschen 版主 2018-04-20
  • 打赏
  • 举报
回复
给出完整代码....
qq_41647611 2018-04-20
  • 打赏
  • 举报
回复
那请问我应该怎么样修改呢?
「已注销」 2018-04-20
  • 打赏
  • 举报
回复
没看懂问什么.... People::getAnswer() 这种方式只有 static 声明的才可以直接调用吧 否则只能通过对象调用
import java.util.*; import java.awt.event.*; import javax.swing.*; import java.awt.*; import java.io.*; public class QuizCardBuilder{ private JTextArea question; private JTextArea answer; private ArrayList cardList; private JFrame frame; public static void main(String[] args){ QuizCardBuilder builder=new QuizCardBuilder(); builder.go(); } public void go(){ frame=new JFrame("Quiz Card Build"); JPanel mainPanel=new JPanel(); Font bigFont=new Font("sanserif",Font.BOLD,24); question=new JTextArea(6,20); question.setLineWrap(true); question.setWrapStyleWord(true); question.setFont(bigFont); JScrollPane qScroller=new JScrollPane(question); qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); answer=new JTextArea(6,20); answer.setLineWrap(true); answer.setWrapStyleWord(true); answer.setFont(bigFont); JScrollPane aScroller=new JScrollPane(answer); qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); JButton nextButton=new JButton("Next Card"); cardList=new ArrayList(); JLabel qLabel=new JLabel("Question:"); JLabel aLabel=new JLabel("Answer"); mainPanel.add(qLabel); mainPanel.add(qScroller); mainPanel.add(aLabel); mainPanel.add(aScroller); mainPanel.add(nextButton); nextButton.addActionListener(new NextCardListener()); JMenuBar menuBar=new JMenuBar(); JMenu fileMenu=new JMenu("File"); JMenuItem newMenuItem=new JMenuItem("new"); JMenuItem saveMenuItem=new JMenuItem("save"); newMenuItem.addActionListener(new NewMenuListener()); saveMenuItem.addActionListener(new SaveMenuListener()); fileMenu.add(newMenuItem); fileMenu.add(saveMenuItem); menuBar.add(fileMenu); frame.setJMenuBar(menuBar); frame.getContentPane().add(BorderLayout.CENTER,mainPanel); frame.setSize(500,600); frame.setVisible(true); } public class NextCardListener implements ActionListener{ public void actionPerformed(ActionEvent ev){ QuizCard card=new QuizCard(question.getText(),answer.getText()); cardList.add(card); clearCard(); } } public class SaveMenuListener implements ActionListener{ public void actionPerformed(ActionEvent ev){ QuizCard card=new QuizCard(question.getText(),answer.getText()); cardList.add(card); JFileChooser fileSave=new JFileChooser(); fileSave.showSaveDialog(frame); saveFile(fileSave.getSelectedFile()); } } public class NewMenuListener implements ActionListener{ public void actionPerformed(ActionEvent ev){ cardList.clear(); clearCard(); } } private void clearCard(){ question.setText(" "); answer.setText(" "); question.requestFocus(); } private void saveFile(File file){ try{ BufferedWriter writer=new BufferedWriter(new FileWriter(file)); for(QuizCard card:cardList){ writer.write(card.getQuestion()+"/"); writer.write(card.getAnswer()+"\n"); } writer.close(); }catch(IOException ex){ System.out.println("couldn't write the cardList out"); ex.printStackTrace(); } } } public class QuizCard { String question; String answer; public QuizCard(String q,String a){ question=q; question=a; } public String getQuestion(){ return question; } public String getAnswer(){ return answer; } }

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧