C++ Problems

1. no default constructor exists for class "Apple"

#include <iostream>
using namespace std;
//默认情况下,类的所有成员都是私有的.只有类和友元函数可以访问私有成员
//protected(受保护)成员在派生类(即子类)中是可访问的
class Box
{
public:
	double length; 
	double Width;
	double GetStr(void)//无参数可以加void,
	{
		return (length + Width)*2;
	}
	double GetArea();//无参数也可以不加void,
};
//类继承
class BoxOne :protected Box
{
	double x;//不指定访问符,默认是Private.
};

double Box::GetArea()
{
	return length * Width;
}


class Apple
{
public:
	Apple(double len);//构造函数声明;无返回值;但可以带参数。
	~Apple();//析构函数声明;无返回值;且无参数;用于释放资源
	double length;
};

Apple::Apple(double len)
{//构造函数是类进入时进行
	cout << "这是构造函数!" << endl;
	length = len;
}

Apple::~Apple()
{//析构函数是类退出时进行
	cout << "这是析构函数!" << endl;
}


int main()
{
	Box box1;
	double area,str;
	box1.length = 2;
	box1.Width = 4;
	area = box1.GetArea();
	str = box1.GetStr();
	cout << "Area:"<< area<<endl;
	cout << "str:" << str << endl;

	Apple apple;
	apple.length = 10;
	cout << "Apple.Length:"<<apple.length << endl;
	cout << "Test 析构顺序!" << endl;
	return 0;
}

报错如下:在这里插入图片描述
纠正措施如下:在C++中要给构造函数的参数赋默认值。

Apple::Apple(double len)

修改为下面形式:

Apple::Apple(double len=0)

2. Error LNK2019 unresolved external symbol __imp__pthread_exit referenced in function _main
Error LNK2019 unresolved external symbol __imp__pthread_create referenced in function _main

使用如下代码发生如上报错:

#include <iostream>
// 必须的头文件
#include <pthread.h>
using namespace std;
#define NUM_THREADS 5

// 线程的运行函数
void* say_hello(void* args)
{
    cout << "Hello Runoob!" << endl;
    return 0;
}

int main()
{
    // 定义线程的 id 变量,多个变量使用数组
    pthread_t tids[NUM_THREADS];
    for (int i = 0; i < NUM_THREADS; ++i)
    {
        //参数依次是:创建的线程id,线程参数,调用的函数,传入的函数参数
        int ret = pthread_create(&tids[i], NULL, say_hello, NULL);
        if (ret != 0)
        {
            cout << "pthread_create error: error_code=" << ret << endl;
        }
    }
    //等各个线程退出后,进程才结束,否则进程强制结束了,线程可能还没反应过来;
    pthread_exit(NULL);
}

报错的原因很简单,编译该cpp时没找到连接的库,需要手动加进来,如下:

  • 打开:https://www.sourceware.org/pthreads-win32/
  • 点击Mirror
    在这里插入图片描述
  • 点击US http
    在这里插入图片描述
  • 找到 pthreads-win32/
  • 下载pthreads-w32-2-9-1-release.zip
  • 解压该文件,将pthreads-w32-2-9-1-release\Pre-built.2\dll\x86文件夹下的pthreadVC2.dll拷到你存放.cpp的文件夹下;将C:\Users\LP\Desktop\pthreads-w32-2-9-1-release\Pre-built.2\lib\x86文件夹下的pthreadVC2.lib也拷贝到你存放.cpp的文件夹下。
  • 在代码中加入预编译命令#pragma comment(lib, "pthreadVC2.lib"),目的是让应用程序预编译时能找到pthread_create等线程函数连接的库
    完整代码如下:
#include <iostream>
// 必须的头文件
#include <pthread.h>
using namespace std;
#pragma comment(lib, "pthreadVC2.lib")
#define NUM_THREADS 5

// 线程的运行函数
void* say_hello(void* args)
{
    cout << "Hello Runoob!" << endl;
    return 0;
}

int main()
{
    // 定义线程的 id 变量,多个变量使用数组
    pthread_t tids[NUM_THREADS];
    for (int i = 0; i < NUM_THREADS; ++i)
    {
        //参数依次是:创建的线程id,线程参数,调用的函数,传入的函数参数
        int ret = pthread_create(&tids[i], NULL, say_hello, NULL);
        if (ret != 0)
        {
            cout << "pthread_create error: error_code=" << ret << endl;
        }
    }
    //等各个线程退出后,进程才结束,否则进程强制结束了,线程可能还没反应过来;
    pthread_exit(NULL);
}

再次运行即可成功。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值