定义了一个类与方法,就是编译不过去,新手,求指导

十年的老股民 2019-08-19 07:36:54
我定义了一个类与方法,就是编译不过去,新手,求指导
实现功能 B.cpp调用A.cpp的方法 并且打印temp_c

//temp.h
#pragma once
#include <iostream>
#ifndef _TEMP_H_
#define _TEMP_H_
class temp
{
public:
temp();
~temp();
void str_way(std::string &str);
private:
};
temp::temp()
{}
temp::~temp()
{}
#endif // !_TEMP_H_


//temp.cpp
#include <string>
#include <iostream>
#include "stdafx.h"
#include "temp.h"
#include <string>
void temp::str_way(std::string &str) {
extern std::string a;
int temp_a = 1;
int temp_b = 2;
int temp_c = 0;
temp_c = temp_a + temp_c;
std::cout << temp_c << std::endl;
a = str;
std::cout << a << std::endl;
std::cout << std::endl;
}


//A.cpp
#include "stdafx.h"
#include "temp.h"
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str_new = "1";
temp temp_new;
temp_new.str_way(str_new);

return 0;
}


图中ConsoleApplication1.cpp为A.cpp


编译报错
...全文
92 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
十年的老股民 2019-08-19
  • 打赏
  • 举报
回复
问题解决:
由于创建了构造,析构函数,需要在cpp里构建构造析构函数即可
十年的老股民 2019-08-19
  • 打赏
  • 举报
回复

楼上的,不对呀
尐东东 2019-08-19
  • 打赏
  • 举报
回复

//temp.h
#pragma once
#include <iostream>
#ifndef _TEMP_H_
#define _TEMP_H_
class temp
{
public:
    temp(){}
    ~temp(){}
    void str_way(std::string &str);
private:
};
#endif // !_TEMP_H_

#include <string>
#include <iostream>
#include "stdafx.h"
#include "temp.h"
#include <string> 
void temp::str_way(std::string  &str) {
    std::string a;  //remove extern
    int temp_a = 1;
    int temp_b = 2;
    int temp_c = 0;
    temp_c = temp_a + temp_c;
    std::cout << temp_c << std::endl;
    a = str;
    std::cout << a << std::endl;
    std::cout << std::endl;
}

33,321

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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