我该如何修复此代码? [英] How do I fix this code?

查看:70
本文介绍了我该如何修复此代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

main.cpp



  #include     Login.h 

int main(){
Loginf();
}





Login.h



  #ifndef LOGIN_H 
#define LOGIN_H
#include < string >
#include < iostream > ;

使用 命名空间标准;

void Loginf();


class 登录
{
public

登录();
string getInput(){
return 输入;
}
void setInput(string x){
x = input;
}
private
字符串输入;
};

#endif





Login.cpp



  #include     Login.h 


登录::登录(){};

void Loginf(){
登录lo;
lo.setInput( hello);
cout<< lo.getInput();
};





我开始使用一个简单的程序来显示屏幕输入。然而,当我构建它时,我得到的只是:



 mingw32-g ++。exe -obin \Debug \\ \ first project.exeinclude \Login.h.gch obj \Debug \ main.o obj \Debug \ src \Login.o 
include \Login.h.gch:file not not识别:文件格式无法识别
collect2.exe:错误:ld返回1退出状态
处理终止,状态为1(0分钟,0秒(秒))
0错误( s),0警告(0分钟,0秒(s))





我有不知道为什么给我这个。这个程序的原因是弄清楚如何使用带有函数原型的头文件和带有对象的类,以便我可以使用简单的登录创建程序。所以,我需要保留函数和类,我不想简单地在main中定义函数或类似的东西。



所以我的问题,总结一下,简单地说:为什么这个代码不会运行,我该怎么做才能修改它而不改变格式(main .cpp调用函数,login.h将其原型化并创建类,login.cpp定义函数)。

解决方案

我不熟悉mingw,但是include\Login.h.gch的引用表明Login.h被用作预编译头。看起来你并不打算将它用作预编译的头文件!我建议你检查项目设置和属性。要么禁用预编译头文件,要么阅读如何正确使用它们并修复预编译头文件定义的设置和内容。


引起我注意的第一件事:函数 setInput 什么都不做。它将值赋给by-value字符串参数,并且在退出时会丢失此赋值的效果。也许你打算分配

 input = x; 



-SA

main.cpp

#include "Login.h"

int main () {
    Loginf();
}



Login.h

#ifndef LOGIN_H
#define LOGIN_H
#include <string>
#include <iostream>

using namespace std;

void Loginf ();


class Login
{
public:

    Login ();
    string getInput () {
        return input;
    }
    void setInput (string x) {
        x=input;
    }
private:
    string input;
};

#endif



Login.cpp

#include "Login.h"


Login::Login () {};

void Loginf () {
    Login lo;
    lo.setInput("hello");
    cout << lo.getInput();
};



A simple program that i started with to display an input to the screen. However, when i build it, all i get is this:

mingw32-g++.exe  -o "bin\Debug\first project.exe" include\Login.h.gch obj\Debug\main.o obj\Debug\src\Login.o
include\Login.h.gch: file not recognized: File format not recognized
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))



I have no idea why its giving me this. The reason for this program is to figure out how to use header files with functions prototypes and a class with an object so that i can create a program with a simple login. So, I need to keep the function and the class, I don't want to have to simply define the function in main or something like that.

So my question, to sum this up, is simply: why won't this code run, and what can I do to fix it without changing the format (main.cpp calls the function, login.h prototypes it and creates the class, login.cpp defines the function).

解决方案

I'm not familiar with mingw, but the reference to include\Login.h.gch indicates that Login.h is being used as a precompiled header. It doesn't look like you intended it to be used as a precompiled header however! I suggest you check the project settings and properties. Either disable the use of precompiled headers, or read up on how to use them properly and fix the settings and contents of your precompiled header definition.


First thing that caught my eye: the function setInput does nothing. It assigns the value to a by-value string parameter, and the effect of this assignment is lost on exit. Probably, you meant to assign

input = x;


—SA


这篇关于我该如何修复此代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆