반응형

비주얼스튜디오 : no appropriate default constructor available,

dev C++ : no matching function for call to 'Block::Block()' 

[Error] no matching function for call to

알고리즘 문제를 풀다가 다음과 같은 에러를 발견했습니다.

 

본 코드는 다음과 같습니다.

#include <bits/stdc++.h>
using namespace std;

struct Block{
	int area;
	int height;
	int weight;
	
	Block(int a, int b, int c){
		area = a;
		height = b;
		weight = c;
	}
	
	bool operator<(const Block &b) const{
		
		return(area > b.area);
	}
	
}; 

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int n;
	cin >> n;
	vector<Block> v(n+1);
	vector<int> ch(n+1);
	
	v[0] = Block(INT_MAX, 0 , INT_MAX);
	
	for(int i=1; i<=n; i++){
		int a, b, c;
		cin >> a >> b >> c;
		v[i] = Block(a,b,c);
	}
	
	sort(v.begin(), v.end());
	
	ch[0] = 0;
	for(int i=1; i<=n; i++){
		int max_height = 0;
		for(int j=i-1; j>=0; j--){
			if(v[i].area < v[j].area && v[i].weight < v[j].weight){
				max_height = max(max_height, ch[j]);
			}
		}
		ch[i] = max_height + v[i].height;
		
	}
	
	int ans =0;
	for(int i=1; i<=n; i++){
		ans = max(ans, ch[i]);
	}
	cout << ans;
	
	
}

 

여기서 에러가 발생한 부분은 다음과 같습니다.

#include <bits/stdc++.h>
using namespace std;

struct Block{
	int area;
	int height;
	int weight;
	
	Block(int a, int b, int c){
		area = a;
		height = b;
		weight = c;
	}
	
	bool operator<(const Block &b) const{
		
		return(area > b.area);
	}
	
}; 

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int n;
	cin >> n;
    //이 부분 
	vector<Block> v(n+1);
	
}

Block 이라는 구조체를 만들고

Block 자료형으로 이루어진 벡터를 만들어

벡터 사이즈를 n+1로 동적으로 초기화해주었는데 오류가 발생했습니다.

 

 

그래서 다음 코드와 같이

벡터 사이즈를 초기화하지 않고 다시 코드를 짜니 오류가 발생하지 않았습니다.

#include <bits/stdc++.h>
using namespace std;

struct Block{
	int area;
	int height;
	int weight;
	
	Block(int a, int b, int c){
		area = a;
		height = b;
		weight = c;
	}
	
	bool operator<(const Block &b) const{
		
		return(area > b.area);
	}
	
}; 

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int n;
	cin >> n;
	vector<Block> v;
	
	
}

 

구조체를 자료형으로 갖는 벡터의 사이즈를 동적으로 초기화해주면 문제가 발생하는가?

 

하는 의문을 갖고 구글링을 했습니다.

 

 

 

컴파일러 오류 C2512

https://docs.microsoft.com/ko-kr/cpp/error-messages/compiler-errors-2/compiler-error-c2512?f1url=%3FappId%3DDev16IDEF1%26l%3DKO-KR%26k%3Dk(C2512)%26rd%3Dtrue&view=msvc-170 

 

컴파일러 오류 C2512

자세한 정보: 컴파일러 오류 C 2512

docs.microsoft.com

"Void가 아닌 매개 변수를 사용 하는 생성자를 제공 하 고 매개 변수 없이

(예: 배열의 요소로) 클래스를 만들 수 있도록 허용 하려면 기본 생성자도 제공 해야 합니다.

기본 생성자는 모든 매개 변수에 기본값을 사용하는 생성자일 수 있습니다."

 

 

 

구조체, 클래스 내에 기본생성자를 구성하지 않고

 

벡터의 사이즈를 동적으로 초기화해주면 이런 오류가 발생할 수 있는 것 같습니다.

 

그래서 해결방법으로는

 

1. 사이즈를 동적으로 초기화 하지 않거나

 

2. 구조체나 클래스에 기본 생성자를 추가해주면 오류가 발생하지 않습니다.

 

 

1번은 위에서 push_back을 이용한 방법이고

 

2번은 다음 코드와 같이 해주면 됩니다.

 

#include <bits/stdc++.h>
using namespace std;

struct Block{
	int area;
	int height;
	int weight;
	
	// 기본 생성자 추가 
	Block(){
	} 
	
	Block(int a, int b, int c){
		area = a;
		height = b;
		weight = c;
	}
	
	bool operator<(const Block &b) const{
		
		return(area > b.area);
	}
	
}; 

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int n;
	cin >> n;
	vector<Block> v(n+1);

	
}

Block(){

}

이라는 기본생성자를 추가해주니 벡터 사이즈를 동적으로 초기화해주어도

 

오류가 발생하지 않았습니다.

 

+ Recent posts